Click Object fires script up hierachy, but affecting ALL objects :(

Imagine a cube you click and it turns over 90 degrees…

I’ve a prefab made of 2 sides of a cube - the sides have a script that checks a mouse-raycast test to see if they’ve been clicked.
The 2 sides are parented to a root node with another script with a bool variable ‘isTurning’
I click on one side, the trigger fires and the bool is updated, so it knows to turn 90 degrees.

This works fine.

I instantiate a grid of these cubes and click one, then they ALL turn over ;(

I’m using this code, but presume it only affects the object clicked?

if (transform.parent.gameObject.tag == “Parent”){ **
** transform.parent.gameObject.GetComponent().isTurning = true;

** }**

Any clues really appreciated, or I am going about it all the wrong way - I’m pretty new to Unity :wink:
Andy

Without knowing the code of BlockHandler, we probably can’t help much.

I’m guessing that isTurning is a static variable. Am I right?

Here’s the BlockHandler - just turns the block if the bool is true

public class BlockHandler : MonoBehaviour {

** public bool isTurning;**

** // Use this for initialization**
** void Start () {**

** }**

** // Update is called once per frame**
** void Update () {**

** if (isTurning){**

** if (gameObject.transform.rotation.x > -0.7f){**
__ transform.Rotate (-90 * Time.deltaTime * 2f, 0, 0);__
** }**

** }**

** }**
}

But it’s not static (which is why all the instantiated prefabs turn?) - if I set it to static, I can’t access it ;(

That all seems correct.

I’m assuming that you’ve got an Empty tagged ‘Parent’ with the cube inside it. The cube has the click script, and the parent has the BlockHandler script. And that’s set up as a prefab. Then you drag a bunch of these prefabs to the screen. And then clicking one makes them all spin?

If that’s all the case, I’m not sure what’s going on there.

That’s exactly how it’s set up, yes. It’s as if it’s finding ALL the objects with a “Parent” tag and setting their ‘IsTurning’ to true.

Just worked it out - I need to specifically use the ‘hit’ object

hit.collider.gameObject.transform.parent.GetComponent().isTurning = true;

Thanks anyways :wink: