Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 captainspaceman · Aug 20, 2017 at 02:26 AM · getcomponentinstanceobject reference

what the heck do you put in the object reference!? all i need is a variable!

many people have asked questions about this but NONE answer my very simple question properly.

i simplified it down as far as i could go. i am simply trying to debug.log the value of a variable from an object by clicking on another object.

public class test : MonoBehaviour {

 public int numToBeAccessed;

 private void Start()
 {
     numToBeAccessed = 10;
 }

}

and the other script:

public class test2 : MonoBehaviour {

 public test numAccessor;

 public void OnMouseDown()
 {
     numAccessor = GetComponent<test>();
     Debug.Log(numAccessor.numToBeAccessed);
 }

}

what the heck do i put in the object reference numAccessor?!

im just trying to debug.log a variable from another script.

the error reads at the debug.log command saying:

NullReferenceException: Object reference not set to an instance of an object test2.OnMouseDown () (at Assets/scripts/test2.cs:12) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

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
0
Best Answer

Answer by TheSOULDev · Aug 20, 2017 at 02:47 AM

How can Unity tell which script you're looking to debug it from? If you want to debug it without a reference, make the numToBeAccessed static and call it as test.numToBeAccessed. Otherwise you'll need to add your script to the same object that holds the script you're trying to access it from.

If you still don't understand why you need to do this, I suggest you read C# manuals and learn what OOP is, otherwise you'll have a hard time adapting to Unity.

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 captainspaceman · Aug 20, 2017 at 03:04 AM 0
Share

well i did that and it worked so thank you i wish i could hug you i was about to give up.

im very new and dont understand why in the dozens of videos and posts i read when searching for "how to access a variable from another script" did none of them just say "oh ya just make it static" you are a legend sir thank you again.

avatar image TheSOULDev captainspaceman · Aug 20, 2017 at 03:29 PM 0
Share

Well, the thing with static variables is that you can only use them for one thing at a time. If you have a script that controls different objects (let's say a movement script that lets different objects move how they want), then making something static won't work the way you want it to. $$anonymous$$aking variables static works only if that variable is used by all users equally and if you only need one instance of it. For everything else, you will need to point to a script you want to take the value from, so if you have 2 or more of the same scripts running on different object, Unity knows which one to look at (they're all different despite being the same class).

avatar image
0

Answer by realcyberleon · Jul 02, 2021 at 08:31 AM

Holy mother of god @TheSOULDev ,

I went - headbanging my head on my keyboard - through half of THIS https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it until I found your solution to my very problem, for I was doing exactly the same thing as @captainspaceman , namely debugging by following a variable from one script through another.

THANKS!

P.S., If you could give us a good (small one-or-few-more-liner-code) hint on how to do what you describe

Blockquote "... you will need to point to a script you want to take the value from, so if you have 2 or more of the same scripts running on different object, Unity knows which one to look at (they're all different despite being the same class)."

that would be much appreciated, at least by the two of us!
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

69 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 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

"Object reference not set to an instance of an object" Error 1 Answer

Gameobject.find & my error NullReferenceException: Object reference not set to an instance of an object 1 Answer

Pause Script Suddenly Stops Working 0 Answers

NullReferenceException Issues 3 Answers

NullReferenceException: Object reference not set to an instance of an object 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