Instantiate: Create Connection

Hello everyone :slight_smile:

I have a small problem that I’m not sure how to fix. In my scene, I have a main character and one cube. I applied a script to the cube that orders it to approach a (GameObject).

When I drag the main character game-object into the (GameObject) slot of the cube script, everything works fine; the cube travels towards the character.

However, when I instantiate the cube PREFAB, I cannot get the (GameObject) variable to refer to the main character in the scene.

I looking for a way of somehow saying:

var travelTo : Transform;
travelTo = ("name_Of_The_Main_Character_GameObject_In_The_Hierarchy");

Thanks for the help :slight_smile:
Muhasaresa
—||—

You could find the main character by tag at Start, like this:

var travelTo: Transform;
function Start(){
  // assuming that the main character is tagged "Player":
  var go: GameObject = GameObject.FindWithTag("Player");
  travelTo = go.transform;
}

You can also find it by name (a bit slower): just use GameObject.Find(“CharacterName”) instead.