I have this cloned object with 2 colliders. One small one, and one big one.
When the big one gets hit, something happens on the parent script, and when the small one gets hit, something different happens on the parent script.
I’ve already tried making a child object and inserting the 2nd collider there, but since the objects are cloned, when the 2nd collider gets hit on this specific clone, the other colliders for the other clones detect it as well (Since I had to make it static, because it was a child object changing variables on the parent object’s script).
So what is the best way to fix this?
I need something to prevent the parent variable from being changed when some other collider gets hit. Only the said collider’s parent should detect it, not the other parents.
Here’s my code so you guys could understand:
Parent
using UnityEngine;
using System.Collections;
public static bool colorRed;
public class Parent : MonoBehaviour {
if(colorRed)
{
Debug.Log ("Red");
}
else
{
Debug.Log ("Blue");
}
}
Child #1
using UnityEngine;
using System.Collections;
public class Child1 : MonoBehaviour {
void OnTriggerEnter(Collider col) {
//If it hits Child1
if (col.gameObject.CompareTag ("colorThing"))
{
//Make it Red
Parent.colorRed = true;
}
}
Child #2
using UnityEngine;
using System.Collections;
public class Child2 : MonoBehaviour {
void OnTriggerEnter(Collider col) {
//If it hits Child2
if (col.gameObject.CompareTag ("colorThing"))
{
//Make it Blue
Parent.colorRed = false;
}
}
So what happens here is, when Child1’s collider gets hit, red happens on parent, and when Child2’s collider gets hit, blue happens on parent.
The problem is, the objects are cloned many times.
When the red one gets hit for this specific clone, the parent of ALL the clones gets red. What I want is, when the specific clone gets red, it’s the only one that gets red, not the other clones.
This could be fixed if I can just have multiple colliders on one object that does 2 different things when the same object collides with them, so I don’t have to make the bool colorRed static.
Thanks for the help