I have an object in the game. It has a script. that script is set to target another game object.
the prefab of this object has the scripts.
how do i set the prefab object to target the game objects i want it to target.
IE an enemy to be instantiated by prefab and target the player.
normally in the hierarchy you drag the player over to the enemy’s “target”
so basically im wondering how to do this in prefab
Find the player by name or tag in the Start function, is the easiest way.
GameObject.FindObjectWithTag( “Player” )
Pretty much the same way. You can drag and drop in the Project view outliner as well as the scene hierarchy outliner
ok so in C# i have this:
public GameObject player;
which sets the object as a variable that you drag and drop.
to replace that i do this?
public GameObject GameObject.FindObjectWithTag( "Player" );
and in java
var scoreGUI : GUIText;
replace with
var scoreGUI : GameObject.FindObjectWithTag( "ScoreGUI" );
?? lol
j_y_k
January 14, 2011, 9:25pm
5
anon_29458414:
ok so in C# i have this:
public GameObject player;
which sets the object as a variable that you drag and drop.
to replace that i do this?
public GameObject GameObject.FindObjectWithTag( "Player" );
No, that code doesn’t make any sense syntactically. What you most likely want to do is acquire the reference in your Awake() or Start() function, as Vicenti suggested.
Java is not a supported language in Unity. (Yes, I know you mean javascript, but the terms are not interchangeable.)
Yeah, javascript… lol.
anyway. the problem is theres another function in the “private pool” or whatever c# does thats calling on that function and moving it generates errors
private Vector3 aimTarget { get { return player.transform.position + new Vector3(0, playerYpos, 0); } }
I dont know c# well enough to move both of them and have it work the same.
I have another post for this specific question. I figured out the javascript one and it worked just fine.
var scoreManager = GameObject.Find("ScoreManager");
scoreManager.GetComponent(ScoreManager).BotDied();