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?
Answer by JVene · Sep 04, 2018 at 08:42 PM
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.
'Create' is a script of an empty object called 'Creator'. Its job is just to instantiate a cube prefab called 'ToBeCreated'. 'Rot' script is in another GameObject that should cause 'ToBeCreated' to move up by using getting the cube variable from the 'Create' script. I could find another way to do this by just finding the name of the instantiated object but I was just wondering if it was possible to use GetComponent to access it.
GetComponent must be called on an instance of a GameObject, and it appears Creator is null, which would cause an exception.
Is 'Create' a singleton? (Is there only 1, and never more than 1)? If so, you can obviate 'find' and initialize a static variable to 'this' when Create instantiates, but such singletons in Unity are a bit of a fudge.
Thank you. I believe I was mistaken in thinking that I could use GetComponent in order to grab an object instantiated in another script since a GameObject isn't a component. Ill research other methods to work around this. There is only one 'Create' script but in my game I am going to want to instantiate multiple instances of the object with different rules. I was going to use this method in order to spawn enemies into a game and have a quick way to access them and change things individually in other scripts.
Your answer
Follow this Question
Related Questions
Referencing a GameObject's Components from if Statements (c#) 0 Answers
Game Manager can't decide what the instance of the object is despite just making an instance of it 1 Answer
Instantiation of my GameObjects spell (from other script) 0 Answers
Change color of object with C# 1 Answer
Gameobject array 0 Answers