#Get component in Sibling?

1 messages · Page 1 of 1 (latest)

lunar copper
#

Hello.
I have a prefab that has in root 2 particlesystem gameobjects, each one with a different script, and a prefab that has a 3rd particlesystem.
The 3rd object has a script with 3 variables for these particlesystems. While it's easy to get the one in the object itself, I am having trouble getting the other 2, because they are neither parents or children.
It is important that the other 2 objects are found as siblings in order to prevent them to be found by other prefabs of the same type.

This is what I've tried so far, but it doesn't work:
GetComponentInParent<Transform>().gameObject.GetComponentInChildren<sphereParticle>().gameObject.GetComponent<ParticleSystem>();
sphereParticle is one of the two scripts I am using to differentiate the 2 particlesystems

nocturne tartan
#

If you can get access to the scripts/components, why not reference the particle systems in them and avoid additional GetComponent calls altogeher?
Also, your line is redundant. transform.parent would accomplish the same as GetComponentInParent<Transform>() while being shorter & more performant. And accessing gameObject is not necessary to call GetComponentInChildren (and neither GetComponent).

lunar copper
nocturne tartan
#

But... you already have scripts on the objects of the particle systems. Different scripts, if I understand your originaly question correctly.
Why not make serialized references to the respective PS in the individual scripts? That way, you not only have get just the scripts, but there also won't be confusion.