- Home /
Add a gameobject/transform present in a scene to a script on runtime
Hi, so my question is not about getting code, I want to know if this is possible and what command can I use. The problem is this one, I have instantiated a gameobject in the game but it uses a script that uses a gameobject or transform that was already in the game scene. I tried using the prefab for the transform but it doesn't work, I need to use the one already present in the game scene, so I am wondering if I can assign it to the script variable after I have instantiated this gameobject.
Answer by robertbu · Apr 30, 2014 at 12:10 AM
If I understand your question correctly, this is the typical way for an instantiated object to initialize a variable from another game object in the scene.
private var player : Transform;
function Start() {
player = GameObject.Find("Player").transform;
// or
player = GameObject.FindWithTag("Player").transform;
}
Your answer
Follow this Question
Related Questions
GameObject references runtime script in scene file. Unsure whats wrong 2 Answers
using WWW to upload prefabs/game objects in webplayer... help 1 Answer
How to use unityeditor and where I need it? 1 Answer
PrefabUtility.ReplacePrefab trouble 0 Answers
How could I save my Gameobject inside a defined Prefab ? 1 Answer