- Home /
Question by
jfalexvijay · Mar 11, 2012 at 07:05 PM ·
gameobjectprefabscene
Dynamically how to detect a gameobject and its position?
Hi, I am new for Unity. I have created a game it contains only one scene. I have created a Prefab, it contains a Cube. Dynamically I am creating reference for Prefab with dynamic position and adding it to a scene. My question is; dynamically how to detect the referenced Prefab (GameObject) and its position? Thanks in advance.
Code:
public class ClassName: MonoBehaviour{
public GameObject myCube;
void Update(){
GameObject cube = myCube;
Instantiate(cube, new Vector3(Random.Range(-10,10), Random.Range(-10,10), z), Quaternion.identity);
}
}
Comment
Answer by kalvinlyle · Mar 11, 2012 at 07:18 PM
If you instantiate the prefab and assign it to a variable then you can check the variable at any point. Something like:
public GameObject cubePrefab;
GameObject cube;
void Start(){
cube = Instantiate(cubePrefab);
}
void Update(){
Debug.Log(cube.transform.position);
}