- Home /
Instantiate: Create Connection
Hello everyone :)
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 :) Muhasaresa ---||---
Answer by aldonaletto · Nov 13, 2012 at 11:31 PM
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.
THAN$$anonymous$$S! That solved the problem :D
$$anonymous$$uhasaresa ---||---
Your answer
Follow this Question
Related Questions
Prefab connection problem 1 Answer
Instantiate without breaking prefab 2 Answers
Network Instantiate PlayerPrefab Problem 2 Answers
How to instantiate a prefab with a script attached? 2 Answers