- Home /
How to force a script to affect only a single instance of the object it is applied to?
Hi, I have a script that will activate objects that are in my scene but disabled upon collision. My problem is that once I hit one of them, it triggers the script on all of the other objects as well, causing all of them to become enabled at once, which is not what I want. I've been struggling with this one for awhile now and haven't been able to find any solid answers. Any help would be appreciated.
Here is my code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ActivateFollower : MonoBehaviour
{
private void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.CompareTag("Leader"))
{
gameObject.transform.GetChild(0).gameObject.SetActive(true);
}
}
}
$$anonymous$$onoBehaviors already only act on the instance they're attached to, unless you broadcast and receive messages to signal events, the way you're doing it in that script should have no problems normally, so the source of this problem lies elsewhere. Are you running other code that may be erroneously setting all of the objects to having the same collision box? Also what game object is this script attached to and how are its children laid out? None of the code in that script should be the problem, so we'd probably need more info to be able to help.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to take my unity skill to the next level 3 Answers
Looking for advice about changing levels. 1 Answer
all instantiated walls face same way 1 Answer