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 herp johnson · Aug 15, 2012 at 09:05 PM · nullreferenceexceptionreference

Scripts help

I have a variable, audioHasPlayed, which is in a script called Rotors. I'm trying to access through another script. Is this how I do it?

Note: It compiles but in Unity it says "NullReferenceException: Object reference not set to an instance of an object."

 private var rotorScript: Rotors;
 private var audioHasPlayed: boolean = rotorScript.audioHasPlayed;
Comment
Add comment · Show 6
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 GC1983 · Aug 15, 2012 at 09:10 PM 0
Share

A NullReferenceException is a very common error. It has to do with you trying to access something and there being no object to access to. So, say you make a GameObject variable and not assign the variable, and then have the script try to reference some kind of execution attached another object, it cant find that GameObject you are wanting to manipulate.

avatar image GC1983 · Aug 15, 2012 at 09:13 PM 1
Share

In your situation, you are calling for a script with rotorScript.audioHasPlayed;. where is the rotorScript attached? You need to do a GameObject.Find().GetComponent(); to assign the rotorScript variable.

avatar image herp johnson · Aug 15, 2012 at 09:43 PM 0
Share

so would this work?

private var rotorscript: rotors = GameObject.Find("FrontRotor").GetComponent(rotors);

private var audioHasPlayed: boolean = rotorscript.audioHasPlayed;

avatar image herp johnson · Aug 15, 2012 at 10:01 PM 0
Share

or would this work? since GameObject.Find() has to be called from Start or some other function?

var rotorscript: rotors;

var audioHasPlayed: boolean = rotorscript.audioHasPlayed;

function Start () {

rotorscript = GameObject.Find("FrontRotor").GetComponent(rotors);

}

avatar image save · Aug 15, 2012 at 11:04 PM 1
Share

You should assign the boolean variable after you've assigned the script. You might as well keep the variable audioHasPlayed static and do this from the helo script:

 if (rotors.audioHasPlayed) DoSomething();
Show more comments

1 Reply

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

Answer by NhommeBeurreOne · Aug 15, 2012 at 09:15 PM

You can't access private variables from other script. Also, you may want to take a look at the Reference. Basically, you need to tell on which GameObject the script is attached to find it.

Best Regards
Nbo

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

11 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

Related Questions

"Object Reference not set to an instance of an object" 1 Answer

NullReferenceException - Maze Generator Script 1 Answer

How do I reference something from another scene? 0 Answers

How Can i Reference String another Script? 2 Answers

How to reference another script and call a function in C# ? 2 Answers


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