Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Luca91 · Jun 03, 2013 at 06:02 PM · gameobjectnullreferenceexception

NullReferenceException while trying to access script on object

Hello,

I've a script attached to a gameobject, this script called "testvar.cs" and contains:

 public int great = 123;

Now I need to change "great" value from 123 to 2, from another script, and I'm using this line:

  playerPrefab.GetComponent<testvar>().great = 2;

playerPrefab is a the public gameobject (set with the inspector)where I've "testvar.cs" script. By the way that line of code always produce NullReferenceException.

Where is the error ? Thanks

Comment
Add comment · Show 7
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 robertbu · Jun 03, 2013 at 06:10 PM 0
Share

Pull it apart to test what is going on:

 if (playerPrefab == null)
   Debug.Log("null playerPrefab");
 
 if (playerPrefab.GetComponent() == null)
   Debug.Log("GetComponent on testvar" failed");

Post the initialization code for playerPref. If you initialize it in the inspector, check to make sure that the script above is not attached to multiple game objects.

avatar image Luca91 · Jun 03, 2013 at 06:20 PM 0
Share

This is what I get:

GetComponent on testvar failed

The strange thing is that the intellisense find the "great" variable ! :(

EDIT: also yes, testvar.cs is attached to other objects, but I need to modify only the one attached to playerPrefab game object !!

avatar image numberkruncher · Jun 03, 2013 at 06:32 PM 0
Share

Intellisense will find the "great" variable because the function is returning an object which would contain it if not null. It seems like your "playerPrefab" does not contain the "testvar" component :)

avatar image Luca91 · Jun 03, 2013 at 06:35 PM 0
Share

O$$anonymous$$ I FOUND IT: if I put the testvar.cs script in the root of the gameobject it works, but if I put the script in a sub gameobject, then it doesn't work. How can I fix this ? I need the script in a sub gameobject :/

avatar image syclamoth · Jun 03, 2013 at 06:37 PM 0
Share

Oh! Well then you need to use

 GetComponentInChildren<testvar>()

ins$$anonymous$$d. Just 'GetComponent' only returns components on that specific object.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Luca91 · Jun 03, 2013 at 07:53 PM

FIXED :D

I've read that Unity 4 handles active totally different, so I fixed it using:

 playerPrefab.GetComponentsInChildren<testvar>(true)[0].great;

Thanks :D

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
1

Answer by darthbator · Jun 03, 2013 at 06:40 PM

If you want to get an object on a child by targeting the parent object you would want to call FindComponentInChildren

Comment
Add comment · Show 1 · 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 Luca91 · Jun 03, 2013 at 06:51 PM 0
Share

It doesnt work :( if I run this code:

 if (playerPrefab.GetComponentInChildren<testvar>() == null)
     Debug.Log("GetComponentInChildren on testvar failed");

I get:

GetComponentInChildren on testvar failed

Damn :/

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

NullReference when accessing GameObject in array (C#) 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Using Scripts in AssetBundles 0 Answers

this.gameObject null reference exception 3 Answers

How to use if statement properly 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