Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Feb 09, 2016 at 12:09 PM by RyanSilva for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by RyanSilva · Feb 07, 2016 at 08:39 PM · getcomponentchild objectenable and disable scriptfindchild

"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 ;)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LLIV · Feb 07, 2016 at 08:59 PM 0
Share

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?

avatar image RyanSilva LLIV · Feb 07, 2016 at 09:50 PM 0
Share

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.

2 Replies

  • Sort: 
avatar image
0
Best Answer

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RyanSilva · Feb 08, 2016 at 01:02 PM 0
Share

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?

avatar image Wrymnn RyanSilva · Feb 09, 2016 at 07:04 AM 0
Share

Ah it`s in JS, well.

  1. Did you try to check if ($$anonymous$$onoBehaviour)child2.GetComponent ("Joystick")) doesnt return NULL right away?

  2. 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"))

Follow this Question

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ChildsParentObject Function 0 Answers

Reference a script on a gameobject that is disabled in scene 0 Answers

Disable a Component with a script. Not working 2 Answers

2 same Scripts w/ GetComponent : one works, one doesn't 2 Answers

Referencing a function in a C# script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges