Greetings,
I’m having issues with getting Getcomponent to work between two scenes, but I’m having the following error:
NullReferenceException: Object reference not set to an instance of an object
Resultpage.Start ()
.
My first scene ‘build’ has the script ‘Build’ attached to its Main Camera.
My second scene ‘resultpage’ has the script ‘Resultpage’ attached to is Main Camera.
The script ‘Build’ has the public int ‘count’ which I want to display in my next scene:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class Build : MonoBehaviour {
public int count = 3;
// Use this for initialization
void Start () {
I want to display ‘count’ in ‘Resultpage’ in a Debug.Log:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Resultpage : MonoBehaviour
public Build scriptBuild;
// Use this for initialization
void Start () {
scriptBuild = gameObject.GetComponent<Build> ();
Debug.Log (scriptBuild.count);
}
The second script recognize scriptBuild.count, which makes me think that the connection has been succesfull. However, when I run the code, it gives me the nullreferenceException.
Have I overlooked something? Ultimately, I wish to transfer values of an ArrayList this way, but I’m first practicing with an int.
Thanks in advance for the help.
Do you have DontDestroyOnLoad() anywhere in your build script?
No, I haven’t used it in any of the script. I have looked it up in the documentation:
“// Make this game object and all its transform children
// survive when loading a new scene.
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}”
I tried filling in ‘Camera.main’ instead of ‘transform.gameObject’ and paste it in the first script, but I still get the same error.
You want to put the Build script on an empty gameobject in your first scene, name the gameobject: “BuildObject”
In the Awake() function, add DontDestroyOnLoad(transform.gameObject);
This will allow the BuildObject gameobject to survive a scene transition.
In your ResultPage script, unless the Build script is already attached to the same gameObject, getComponent() will return null.
To get access to the build script that survived across the scene change, you can use:
GameObject.Find("BuildObject").getComponent<Build>();
You will run into problems if you have a gameloop whereby you Travel from: Build → results → build as DontDestroyOnLoad() does not destroy any previous occurences of the object for you, you will have to do that yourself.
Thank you A.Killingbeck!
I managed to solve it by indeed connecting the first script to a gameobject, adding DontDestroyOnLoad(transform.gameObject); to it…
Next I used the code you provided me to connect to it.
Again many thanks, I was stuck with it for quite some time.
Np, if you are feeling adventurous, you could look into the Singleton pattern. It will allow you to get rid of the search by string in GameObject.Find(which I really don’t like btw
)
I will do that, thank you 
I really suggest reading through http://gameprogrammingpatterns.com/ actually; the Singleton has it’s uses, but it does tend to be overused by the game development community at large. I still use it a ton myself, but the more patterns you know and understand the more tools you’ll have in your belt.