Unassigned Reference when using GetComponent on Instantiated Objects,How do I use Get_Component on instantiated objects

I am working on two scripts, one that instantiates an object and another in a different object that makes the newly instantiated object move up. The problem is, when I instantiate a clone, GetComponent doesn’t seem to find it and I get a Unassigned Reference Exception. Here is the code where I instantiate the object.

using UnityEngine;
using System.Collections;

public class Create : MonoBehaviour {
	public GameObject cube;//not defined
	public GameObject prefabcube;
	public int test;
	// Use this for initialization
	void Start () {
		cube = Instantiate (prefabcube, new Vector3 (transform.position.x,transform.position.y,transform.position.z), Quaternion.identity) as GameObject;
		test=5;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

Here is the script in a different object that should make the object move upward

using UnityEngine;
using System.Collections;

public class Rot : MonoBehaviour {
	private Create sc;
	public GameObject Creator;
	// Use this for initialization
	void Start () {
		sc = Creator.GetComponent<Create> ();
		Debug.Log("test= "+sc.test+"");
	}
	
	// Update is called once per frame
	void Update () {
		sc.cube.transform.Translate (Vector3.up);
		sc.test-=1;
	}
}

Does anyone know why this is happening and how I could fix it? Thanks.

,I am trying to access an object that I have instantiated in another script with GetComponent. Unfortunately, I am getting an Unassigned Reference Exception. It says that i probably need to assign the cube variable in the inspector but I want to assign it at run-time once the object has been instantiated. Here is the instantiation code.

using UnityEngine;
using System.Collections;

public class Create : MonoBehaviour {
	public GameObject cube;//not defined
	public GameObject prefabcube;
	public int test;
	// Use this for initialization
	void Start () {
		cube = Instantiate (prefabcube, new Vector3 (transform.position.x,transform.position.y,transform.position.z), Quaternion.identity) as GameObject;
		test=5;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

This is the code that I use to get components of cube and make it move up.
using UnityEngine;
using System.Collections;

public class Rot : MonoBehaviour {
	private Create sc;
	public GameObject Creator;
	// Use this for initialization
	void Start () {
		sc = Creator.GetComponent<Create> ();
		Debug.Log("test= "+sc.test+"");
	}
	
	// Update is called once per frame
	void Update () {
		sc.cube.transform.Translate (Vector3.up);
		sc.test-=1;
	}
}

Does anyone know why it is not finding the clone object for cube and how I can fix this?

As posted, I’d expect the line in Rot.Start that calls GetComponent on Creator to crash because I don’t see how anything is assigned to Creator (it should be null).


It seems from reading your post you may be confusing GetComponent with finding objects, but I’m not sure. To figure that part out, what object is Create a script of? Is that a GameObject named Creator?. You may have to find that GameObject first to get the component out of it.