- Home /
Find a specific object even though there are many of them.
In my scene there are a lot of cactus prefabs, with a explosion particle as children. They all have the same name. I have a script attached to each cactus saying if i hit it, then a explosion will happen. This is my script:
#pragma strict
var emission = 100;
function Start () {
//Emission starts at 0, and when you hit the cactus it gets to 100.
gameObject.particleSystem.emissionRate = 0;
}
function Update () {
if(GameObject.Find("Cactus3").GetComponent(Explode).explosion == true) {
//if i hit the cactus this will happen
print("HI");
gameObject.particleSystem.emissionRate = emission;
}
}
Since all of the cactuses are names cactus3 then all the explosion in the scene will detonate at same time even though i only hit one.
Can i say someting like if(GameObject.FindParent("Cactus3").GetComponent(Explode).explosion == true) {
for example if i have a child of a parent. This child has a script attached. In this script it says something like: if parent.explosion = true, then something.
I dont know how to explain this probably so if you have questions please ask.
For future posts, please format your code. After pasting your code, select it and use the 101/010 button. I did it for you this time.
Answer by Immanuel-Scholz · Nov 17, 2014 at 03:13 PM
You can access your direct parent via transform.parent
. So if the explosion prefab is a direct child of the cactus, just do
if (transform.parent.GetComponent(Explode).explosion)
particleSystem.emissionRate = emission;
I'd like to also point out for OP that accessing the parent this way will be much faster computationally then GameObject.Find(). Typically when you are thinking about using GameObject.Find() in an update you should try to rethink how you access that object as Find() is very slow. I think ideally you should cache Explode ahead of time to make the code both faster and cleaner. Just suggestions for OP and not required for solving the problem.
Answer by Bunny83 · Nov 17, 2014 at 03:39 PM
Like GetComponentInChildren Unity now provides a method called GetComponentInParent. This will search up the hierarchy for that specific component. In your case I would suggest:
var emission = 100;
var explode : Explode;
function Start ()
{
//Emission starts at 0, and when you hit the cactus it gets to 100.
gameObject.particleSystem.emissionRate = 0;
explode = GetComponentInParent(Explode);
}
function Update ()
{
if(explode.explosion)
{
print("HI");
gameObject.particleSystem.emissionRate = emission;
}
}
However, if your cactus is a prefab, you simply should assign the "Explode" script to tha variable in the inspector. When instantiating a prefab such internal references will point to the internal object.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
parent child relation btwn gammeobject and a ui canvas and ui text positioning problem 1 Answer
Creating new Transform from existing objects Transform to Instantiate object 1 Answer
How can I Instantiate a clone as a child of another object, and inherit the parent object's scale? 2 Answers
Need help dealing with unruly children! 2 Answers