- Home /
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)
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
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)
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;
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.
Thank you getyour411, once again. Also, thank you Bunny83 for clearing that up with me. Everything works fine now! :)
Your answer
Follow this Question
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