- Home /
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
You cannot access a script attached to another object in a separate scene. The reason being that the object the script is attached to does not exist and therefore the reference does not exist. The best way to go about this would be to call DontDestroyOnLoad on the object holding the new scenes values. Then load in the new scene, change the necessary values and destroy the old object.