- Home /
Access Prefab Object Via Scirpt
For my game I want to access game object that was created using prefabs. I don't want to use following statement
GameObject.Find("Bird(Clone)");
because it sometime refer to old reference of an object. So what is other way to access clone of prefab?
EDIT
Above image display my current hierarchy window. In this Bird(Clone) is my prefab generated object which has BirdScript attached to it.
GameOjbect has BirdController script attached through which I am trying to get access of Bird object clone. In this I want help from you guys.
Depends on your game mechanics. For example, don't use find at all and only drive these interactions with OnTrigger and the collider.gameObject.... What are you doing that requires GO.Find of an instantiated prefab?
At present, I want to change bird object property. Script I am currently writing that game object is completely different that bird object.
Still don't know what you are doing, which might provide an answer to your question. How does the gameobject you are working on become "aware" of the instantiated bird object - - is it made "aware" of the bird by a raycast, spherecast, trigger, collision, general distance check etc?
Answer by jorjdboss · Jan 18, 2014 at 10:02 AM
Create a global script like so:
public class GLOBAL : MonoBehavior
{
public static GLOBAL instance;
List<GameObject> myBirds;
void Awake()
{
instance = this;
}
}
From one script you can add:
var newB = Instantiate(bird,pos,rot) as GameObject;
GLOBAL.instance.myBirds.Add(newB)`
From another script you can access this global property at no performance overhead.
Answer by getyour411 · Jan 18, 2014 at 09:31 AM
Did the BirdController script spawn the Bird prefab? If so change the line that has instantiate(bird,pos,rot) to
GameObject myBird = Instantiate(bird,pos,rot);
and now the Birdcontroller has full access to all GObj properties via myBird.
Yes, BirdController script spawn new birds. So using above way I can get access the bird instance. But what if BirdController script not generating any birds and that was the task of other script?
Your answer
Follow this Question
Related Questions
At same positioned game objects collision detection 0 Answers
In sequence position game objects 1 Answer
Game Development Approach 0 Answers
Object dragging at manual point 2 Answers
GameObjects creation within boundry 3 Answers