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
1
Question by Halo500 · Feb 18, 2014 at 04:55 AM · accessing from any scriptanother scriptobject-reference-error

Access a Script from Another Scene? "Object Reference not set..."

Hello! I have a Main Menu, and I'm trying to change the skybox and fog in a level if you decide to choose so on the Main Menu. The script for the main menu is here:

     if ( GUI.Button( Rect( 775, 435, 100, 50 ), "$20 Mode!" ) )
     {
         //Set the time back to 1:
         Time.timeScale = 1;
         //Time.timeScale is back to normal, now load the scene:
         Application.LoadLevel( "DayLevel1" );
         var DayLevel = GetComponent(DayLevel); 
         DayLevel.enabled = true;
     }

I have an Empty GameObject with the "DayLevel.js" unchecked

 #pragma strict
 
 public var dayMaterial:Material;
 public var nightMaterial:Material;
 
 function Start () 
 {
     RenderSettings.skybox = nightMaterial;
     //RenderSettings.skybox = dayMaterial;
     RenderSettings.fog = true;
     //RenderSettings.fog = false;
 }
 
 function Awake () {
         DontDestroyOnLoad (transform.gameObject);
     }

I get this error in the console, and the script will not be checked on:

 NullReferenceException: Object reference not set to an instance of an object
 Maps.OnGUI () (at Assets/_Scripts_/Maps.js:40)
Comment
Add comment · Show 4
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 getyour411 · Feb 18, 2014 at 05:00 AM 2
Share

In the top bit, don't put things you want to run below the Application.Loadlevel. It's not clear from this what is line 40 from $$anonymous$$aps.js

avatar image Halo500 · Feb 18, 2014 at 05:01 AM 0
Share

Line 40 is

 DayLevel.enabled = true;
avatar image getyour411 · Feb 18, 2014 at 05:04 AM 2
Share

is DayLevel attached to the same GameObject as whatever the top script is attached to? If not, you'll need to expand the reference to something like var DayLevel = GameObject.Find("...").GetComponent(DayLevel)

avatar image Halo500 · Feb 18, 2014 at 05:16 AM 0
Share

Ugh, now I got this error:

 Assets/_Scripts_/$$anonymous$$aps.js(38,23): BCE0020: An instance of type 'UnityEngine.Behaviour' is required to access non static member 'enabled'.

It's still this line:

 DayLevel.enabled = true;

1 Reply

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

Answer by Bunny83 · Feb 18, 2014 at 05:26 AM

Doesn't this line look strange to you:

     var DayLevel = GetComponent(DayLevel);

You named the variable exactly the same as the script. How should the compiler know what you mean. Imagine you would "replace" all words in the english language with the word "DayLevel". Would you understand this sentence: "DayLevel DayLevel DayLevel DayLevel DayLevel DayLevel DayLevel DayLevel DayLevel DayLevel DayLevel?" :D

Try something like this:

     var dayLevelInstance = GetComponent(DayLevel);
     dayLevelInstance.enabled = true;

Also make sure your "DayLevel" script is attached to the same GameObject as your "Maps" script.

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 Halo500 · Feb 18, 2014 at 05:32 AM 0
Share

Thank you getyour411, once again. Also, thank you Bunny83 for clearing that up with me. Everything works fine now! :)

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

20 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

Related Questions

Access a Script from Another Scene? "Object Reference not set..." 0 Answers

GetComponent Trolling Me? 1 Answer

EnemyAi script 1 Answer

Trouble with calling a variable from another script 5 Answers

Getting Monobehavior script from inspector 0 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