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 /
avatar image
0
Question by deeredman1991 · Jul 10, 2011 at 10:58 PM · c#variablenullreferenceexceptiongetcomponent

I'm having more problems accessing a variable from another script in c#

ok guys this is what I have pertaining too this specific function except I have a variable called isstuned saved in the file PlayerScript and it is marked as a public int variable

 void Update () 
     {
         PlayerScript Pscript = GetComponent<PlayerScript>();
         if(Pscript.isstuned == 0)
         {

this is the new compile error I am getting when I hit play to test the game

NullReferenceException: Object reference not set to an instance of an object PlayerAnimation.Update () (at Assets/Scripts/PlayerAnimation.cs:24)

my question is how do I set it to an instance of an object? or is that not what I am supposed to be doing at all?

Comment
Add comment
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

2 Replies

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

Answer by aldonaletto · Jul 10, 2011 at 11:47 PM

The problem is in the PlayerAnimation.cs script, at line 24. What is in that line? if(Pscript.isstuned == 0) ? If so, the problem is that GetComponent is not finding the script PlayerScript, and when you try to use isstuned Unity complains because Pscript == null. If PlayerScript is in another object, you must first find that object, then get the script. If the PlayerScript is attached to the Player object:

 void Update () 
 {
     GameObject obj = GameObject.Find("Player");
     PlayerScript Pscript = obj.GetComponent<PlayerScript>();
     if(Pscript.isstuned == 0)

The Player object must not be a child of other object, or Find will not find it.

Comment
Add comment · Show 3 · 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 deeredman1991 · Jul 11, 2011 at 12:06 AM 0
Share

the game object containing the variable isstuned is the parent of the object with the script I'm working on :)

avatar image deeredman1991 · Jul 11, 2011 at 12:12 AM 0
Share

nvm I figured it out :D ty ^_^ ur awesome dude you've answered 3 of my questions and gotten correct answer on all of them...you've always explained things the simplest and effective way possible...keep it up :D

avatar image sdgd · Jan 19, 2013 at 03:35 AM 0
Share

finally I found the correct answer for the how to access other variables in other scripts after 3 hours searching through all Q/A of how to access other variables in other scripts

you answered my Q 2 times 1. actively 2. time passively

I'm going to make a Q to make a better manual for this Question

I would thumbs up if I could tho :)

avatar image
1

Answer by Bunny83 · Jul 10, 2011 at 11:46 PM

GetComponent will return null when there is no such Component on the GameObject. Are you sure that you have the PlayerScript attached to the same GameObject as this script above?

If you want them on seperate GOs you have to call GetComponent on the right GameObject.

eg.

 public GameObject playerObject;

 void Update () 
 {
     PlayerScript Pscript = playerObject.GetComponent<PlayerScript>();
     if(Pscript.isstuned == 0)
     {

In this case you have to assign the other GameObject to the public variable playerObject in the inspector. It would be easier to directly setup a reference to the PlayerScript.

 public PlayerScript Pscript;

 void Update () 
 {
     if(Pscript.isstuned == 0)
     {

Like in the first example just drag the playerobject onto the variable. Unity will automatically link the PlayerScript instance (if there is one) from that GameObject to the variable.

If the objects are all instantiated at runtime you have to find the right one either by saving the created instance somewhere when you instantiate it, or use one of the search functions like GameObject.Find / FindObjectOfType / ...

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GetComponent with variable script possible? 1 Answer

Object reference not set to an instance of an object in c#. 0 Answers

Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 1 Answer

How can I check a variable in one script from another one in c#? 2 Answers

Enemy variable remains null 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