The question is answered, right answer was accepted
"NullReferenceException: Object reference not set to an instance of an object " only for one component
I can't figure this out at all. Here's the object Hierarchy: http://i.imgur.com/TzVFuJX.png
The script is being executed from "_Scripts" and it's working fine for ONE of the children of FPSPlayerMP10. [note myPlayerGO is the FPSPlayerMP11]
GameObject child1 = myPlayerGO.transform.FindChild ("Player").gameObject;
((MonoBehaviour)child1.GetComponent ("FirstPersonControl")).enabled = true;
//Debug.Log ("Child1 Name: " + child1.name);
GameObject child2 = myPlayerGO.transform.FindChild ("RightTouchPad").gameObject;
Debug.Log ("Child 2 Name: " + child2.name);
((MonoBehaviour)child2.GetComponent ("Joystick")).enabled = true;
So, the child called "Player" has a scrip called "FirstPersonControl" and when the game starts it detects the child and enables the script.
However, this does not work for the 2nd child which is called "RightTouchPad"(gameobject that has a script called "Joystick") ,It does detect it and I get the Debug.Log in the console but for some reason it can't find the "Joystick" Script and enable it [http://i.imgur.com/uvcF5Zz.png].
It gives me "NullReferenceException: Object reference not set to an instance of an object" , and it stops the script from going forward.
It works like charm for the "Player" Child why doesn't it work for the second one?
Thank's for reading and in anticipation for helping ;)
I would assume that it's a misspelling error but in this case I don't think thats it. Try looking for child2 first see if you get the same problem. If you do it's not a problem with this script. Also I noticed your prefab looks like it has some unsaved changes "Select, Revert, Apply" are you sure the script is on the object during runtime?
I've tried everything you suggested and everything looks fine but the error is still there.Looking for child2 before looking for child 1 has the same result, same error. The script is on the object during runtime.
Answer by RyanSilva · Feb 09, 2016 at 12:08 PM
Thank's for trying to help me but I found a work-around that seems to be working fine.Instead of trying to enable the disabled script on RightTouchPad and LeftTouchPad, I've enabled the script and disabled the RightTouchPad and LeftTouchPad game objects.
GameObject child2 = myPlayerGO.transform.FindChild ("RightTouchPad").gameObject;
GameObject child3 = myPlayerGO.transform.FindChild ("LeftTouchPad").gameObject;
child2.SetActive (true);
child3.SetActive (true);
Answer by Wrymnn · Feb 08, 2016 at 09:29 AM
Try getting the component with GetComponent().
Also try to check if the component is found.
Joystick joystick = child2.GetComponent<Joystick>();
Debug.Log("Joystick is: " + joystick );
Also why are you casting joystick as MonoBehaviour? Cast it as Joystick and enable it that way.
The joystick script is written in JS. [I'm just starting into c#,:) ] . Joystick joystick = child2.GetComponent<Joystick>();
, it gives me: "Assets/Network$$anonymous$$anagerR.cs(71,17): error CS0246: The type or namespace name `Joystick' could not be found. Are you missing a using directive or an assembly reference?" How do I declare it properly ? I have to tell the script what is the JoyStick or what?
Ah it`s in JS, well.
Did you try to check if ($$anonymous$$onoBehaviour)child2.GetComponent ("Joystick")) doesnt return NULL right away?
I haven`t worked with JS, but can you also try to cast Joystick as Joystick ins$$anonymous$$d of $$anonymous$$onoBehaviour? As: (Joystick)child2.GetComponent ("Joystick"))