2

Please help!
I have a Javascript component that references 2 game objects. The problem is, the game objects don't exist at start up and only instantiate when the player joins a network room. The network manager script is in C#.
I need to update the JS script from the C# script when the player is instantiated.
The code I have so far...

            SpawnSpot mySpawnSpot = spawnSpots [Random.Range (0, spawnSpots.Length)];
            GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate ("PlayerSHIP", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
            standbyCamera.SetActive (false);

            ((MonoBehaviour)myPlayerGO.GetComponent ("PlayerControl")).enabled = true;
            ((MonoBehaviour)myPlayerGO.GetComponent ("PlayerShooting")).enabled = true;
            myPlayerGO.transform.FindChild ("CameraCP").gameObject.SetActive (true);
            myPlayerGO.transform.FindChild ("LocalShip").gameObject.SetActive (true);
            myPlayerGO.transform.FindChild ("RemoteShip").gameObject.SetActive (false);
            GameObject test = GameObject.Find ("RadarMgr");
            Component test2 = test.GetComponent("3DRadar");
            test2.Transforms[0] = "LocalShip";
            //test.GetComponent("Radar_Mgr").Transforms[0] = "LocalShip";

This is the network C# script. The JS script I'm wanting to access, is the "3DRadar" component which is part of the "RadarMgr" game object. The "Transforms[0]" is the part of the JS script that holds the game object needed and "LocalShip" is the game object I'm trying to tell the JS script to use. The last 2 lines are my failed attempts at assigning the LocalShip to the JS script. I did have other syntax instead of those 2 lines, but nothing has worked and that is just how it got left.
The other object needed would be the "CameraCP" object, but obviously if I could sort out how to do 1, the other would be done the same way.
I have searched for the last 3 days on here, unity answers and general google, but if the answer is out there somewhere, I cannot find it nor understand it.
I hope someone can shed some light on this frustrating hurdle!
Thanks!

4
  • What error do you get? It will help track down what you are doing wrong. Did it find the GameObject etc? Also try Component test2 = test.GetComponent("3DRadar") as component("3DRader"); Commented Nov 3, 2014 at 12:58
  • I'm not sure if any of the last 4 lines worked to be honest, as I couldn't get any syntax error free for the last lines. I'll look at what the errors are again now, but I don't remember some of the syntax i tried before Commented Nov 3, 2014 at 13:01
  • The main error is to do with "Transforms[0]" and I get "UnityEngine.Component does not contain a definition for Transforms". "Transforms[0]" is what I'm looking to assign a game object to. The JS script has this..."Transforms[0] = GameObject.Find("_Player").transform;" but that is a sort of placeholder. Commented Nov 3, 2014 at 13:11
  • if I print test & test2, I get "RadarMgr (UnityEngine.GameObject)" & "RadarMgr (3DRadar)", so I presume it's doing that part ok Commented Nov 3, 2014 at 14:17

2 Answers 2

1

You are declaring it as a component not a 3DRadar type. You must get a bit more specific.

3DRadar test2 = test.GetComponent("3DRadar") as 3DRadar;

I do not remember the exact syntax but I believe that is it.

or

3DRadar test2 = test3D.GetComponent<3DRadar>();
Sign up to request clarification or add additional context in comments.

8 Comments

I don't understand this answer. If I use your example syntax, I get "unexpected symbol 'Radar' ". As far as I know though, I am already getting the game object and component but not able to assign my game object to Transforms[0]. I may be misleading you with the statement that "the last 2 lines failed"...I included the line that is commented out as 1 of the 2. I'm sure I only need to know the correct syntax for assigning "LocalShip" game object to "Transforms[0]"
@MajorParts: You probably have your UnityScript and C# scripts in the same compilation group. That won't work since C# doesn't know the types defined in your UnityScript file until it's compiled. You named your component "3DRadar"? That's actually an illegal classname since classnames must not start with a number. Try placing your UnityScript (JS) file in the first compilation group ("/Standard Assets/", "/Pro Standard Assets/" or "/Plugins/")
No, it isn't actually called "3DRadar"...it is called a much longer weird name so I shortened it here for clarity. I was already told where to place certain folders and files of this asset if I was using C#, but I don't get to compile stage because of the incorrect syntax involving "Transforms[0]"
The problem is that you are assigning your script to a Unity Engine component. While all scripts are components a component has no idea about what you declared in your script. It is just the base class. The error makes this pretty clear. "UnityEngine.Component does not contain a definition for Transforms" Instead of assigning it to a component base class you need to assign it to a 3DRadar class. Read the doc: docs.unity3d.com/ScriptReference/GameObject.GetComponent.html you will notice that they are not assigning it to a component.
ok, I can understand that up until "assign it to a 3DRadar class". The 3Dradar script is javascript and doesn't have "class" as per a c# script (example "public class NetworkManager : MonoBehaviour". Is this even what you mean? I apologise for being a total amateur at this! I have read and re-read that doc, but it seems to me that "HingeJoint" is a totally different thing to a script component
|
1

This is the syntax I was after....

GameObject temp = GameObject.Find ("player(Clone)/LocalSHIP");
GameObject.Find("gameMgr").GetComponent<radarScript>().Transforms[0] = temp.transform;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.