Ok, I’m fairly new to coding and i have searched everywhere for the answer to this but i can’t find it. I’m simply trying to make a GameObject (instantiated clone) from one script follow a GameObject (instantiated clone) from another script. here is what i have.
I have 2 scripts: scriptA & scriptB.
In script A I instantiate a gameObject called player:
public GameObject player;
void Start() {
GameObject player = Instantiate (player, new Vector3 ((int)Random.Range(-6f,6f), (int)Random.Range(-4f,4f), 0), Quaternion.identity);
}
// does it matter that my prefab is called player?
// it looks strange that i have player as a variable, then use player again in Instantiate?
Now, i want to access that instantiated object (the clone), from scriptB, so in scriptB I have:
public GameObject target; //i drag my prefab into this slot in unity
void Update () {
this.transform.position = target.transform.position; // i was hoping this would make “this” transform always be the same as target.transform
}
the problem i have is that this.transform.position is accessing the transform.position of my prefab, 0,0,0. How do i get the clones position and not the prefab’s position?
At some point in this process i was able to make the this.transform.position equal to the transform.position of player…the problem was it was changing the transform.position of my prefab. So the next time i ran my program the prefab started out at whatever it’s last position was on the last run.
I know this is probably simple, but for the life of me i can’t figure this out. It’s like I only know how to access the prefabs, is that because i am dragging my prefabs into the slots in Unity?
Thank you in advance for any help.
James