- Home /
Why does one script not work on multiple gameobjects?
I have applied a C# script to a game object that enables / disables other game objects as they enter its trigger and it works fine. However when i copy and paste that game object or rebuild it so there is now two of the trigger objects and apply the same script to the second trigger object it registers true on the triggers but it wont run enable / disable the other game objects although the first game object will remain working just fine .
If I disable the original game object the copy works just fine?
using UnityEngine;
using System.Collections;
public class RingHook : MonoBehaviour {
public bool ringOneTrigger ;
public bool ringTwoTrigger ;
public bool ringThreeTrigger ;
public bool ringFourTrigger ;
public bool ringFiveTrigger ;
public bool ringSixTrigger ;
public bool ringSixTriggerB ;
public bool ringSevenTrigger ;
public bool ringEightTrigger ;
public bool ringNineTrigger ;
public bool ringTenTrigger ;
public GameObject ring1;
public GameObject ring1BoxCollider;
public GameObject ring1SphereCollider;
public GameObject ring2;
public GameObject ring2BoxCollider;
public GameObject ring2SphereCollider;
public GameObject ring3;
public GameObject ring3BoxCollider;
public GameObject ring3SphereCollider;
public GameObject ring4;
public GameObject ring4BoxCollider;
public GameObject ring4SphereCollider;
public GameObject ring5;
public GameObject ring5BoxCollider;
public GameObject ring5SphereCollider;
public GameObject ring6;
public GameObject ring6BoxCollider;
public GameObject ring6SphereCollider;
public GameObject ring7;
public GameObject ring7BoxCollider;
public GameObject ring7SphereCollider;
public GameObject ring8;
public GameObject ring8BoxCollider;
public GameObject ring8SphereCollider;
public GameObject ring9;
public GameObject ring9BoxCollider;
public GameObject ring9SphereCollider;
public GameObject ring10;
public GameObject ring10BoxCollider;
public GameObject ring10SphereCollider;
public GameObject lowHookTrigger;
public GameObject highHookTrigger;
// Update is called once per frame
void Awake () {
ring1 = GameObject.FindWithTag ("Ring1");
ring1BoxCollider = GameObject.FindWithTag ("Ring1BoxCollider");
ring1SphereCollider = GameObject.FindWithTag ("Ring1SphereCollider");
ring2 = GameObject.FindWithTag ("Ring2");
ring2BoxCollider = GameObject.FindWithTag ("Ring2BoxCollider");
ring2SphereCollider = GameObject.FindWithTag ("Ring2SphereCollider");
ring3 = GameObject.FindWithTag ("Ring3");
ring3BoxCollider = GameObject.FindWithTag ("Ring3BoxCollider");
ring3SphereCollider = GameObject.FindWithTag ("Ring3SphereCollider");
ring4 = GameObject.FindWithTag ("Ring4");
ring4BoxCollider = GameObject.FindWithTag ("Ring4BoxCollider");
ring4SphereCollider = GameObject.FindWithTag ("Ring4SphereCollider");
ring5 = GameObject.FindWithTag ("Ring5");
ring5BoxCollider = GameObject.FindWithTag ("Ring5BoxCollider");
ring5SphereCollider = GameObject.FindWithTag ("Ring5SphereCollider");
ring6 = GameObject.FindWithTag ("Ring6");
ring6BoxCollider = GameObject.FindWithTag ("Ring6BoxCollider");
ring6SphereCollider = GameObject.FindWithTag ("Ring6SphereCollider");
ring7 = GameObject.FindWithTag ("Ring7");
ring7BoxCollider = GameObject.FindWithTag ("Ring7BoxCollider");
ring7SphereCollider = GameObject.FindWithTag ("Ring7SphereCollider");
ring8 = GameObject.FindWithTag ("Ring8");
ring8BoxCollider = GameObject.FindWithTag ("Ring8BoxCollider");
ring8SphereCollider = GameObject.FindWithTag ("Ring8SphereCollider");
ring9 = GameObject.FindWithTag ("Ring9");
ring9BoxCollider = GameObject.FindWithTag ("Ring9BoxCollider");
ring9SphereCollider = GameObject.FindWithTag ("Ring9SphereCollider");
ring10 = GameObject.FindWithTag ("Ring10");
ring10BoxCollider = GameObject.FindWithTag ("Ring10BoxCollider");
ring10SphereCollider = GameObject.FindWithTag ("Ring10SphereCollider");
}
void OnTriggerEnter (Collider other)
{
if (other.tag == "Ring1")
{
ringOneTrigger = true;
}
if (other.tag == "Ring2")
{
ringTwoTrigger = true;
}
if (other.tag == "Ring3")
{
ringThreeTrigger = true;
}
if (other.tag == "Ring4")
{
ringFourTrigger = true;
}
if (other.tag == "Ring5")
{
ringFiveTrigger = true;
}
if (other.tag == "Ring6")
{
ringSixTrigger = true;
}
if (other.tag == "Ring7")
{
ringSevenTrigger = true;
}
if (other.tag == "Ring8")
{
ringEightTrigger = true;
}
if (other.tag == "Ring9")
{
ringNineTrigger = true;
}
if (other.tag == "Ring10")
{
ringTenTrigger = true;
}
}
void OnTriggerExit (Collider other)
{
if (other.tag == "Ring1")
{
ringOneTrigger = false;
}
if (other.tag == "Ring2")
{
ringTwoTrigger = false;
}
if (other.tag == "Ring3")
{
ringThreeTrigger = false;
}
if (other.tag == "Ring4")
{
ringFourTrigger = false;
}
if (other.tag == "Ring5")
{
ringFiveTrigger = false;
}
if (other.tag == "Ring6")
{
ringSixTrigger = false;
}
if (other.tag == "Ring7")
{
ringSevenTrigger = false;
}
if (other.tag == "Ring8")
{
ringEightTrigger = false;
}
if (other.tag == "Ring9")
{
ringNineTrigger = false;
}
if (other.tag == "Ring10")
{
ringTenTrigger = false;
}
}
void Update()
{
if (ringOneTrigger == true)
{
ring1BoxCollider.SetActive (true);
ring1SphereCollider.SetActive (false);
}
else
{
ring1BoxCollider.SetActive (false);
ring1SphereCollider.SetActive (true);
}
if (ringTwoTrigger == true)
{
ring2BoxCollider.SetActive (true);
ring2SphereCollider.SetActive (false);
}
else
{
ring2BoxCollider.SetActive (false);
ring2SphereCollider.SetActive (true);
}
if (ringThreeTrigger == true)
{
ring3BoxCollider.SetActive (true);
ring3SphereCollider.SetActive (false);
}
else
{
ring3BoxCollider.SetActive (false);
ring3SphereCollider.SetActive (true);
}
if (ringFourTrigger == true)
{
ring4BoxCollider.SetActive (true);
ring4SphereCollider.SetActive (false);
}
else
{
ring4BoxCollider.SetActive (false);
ring4SphereCollider.SetActive (true);
}
if (ringFiveTrigger == true)
{
ring5BoxCollider.SetActive (true);
ring5SphereCollider.SetActive (false);
}
else
{
ring5BoxCollider.SetActive (false);
ring5SphereCollider.SetActive (true);
}
if (ringSixTrigger == true)
{
ring6BoxCollider.SetActive (true);
ring6SphereCollider.SetActive (false);
}
else
{
ring6BoxCollider.SetActive (false);
ring6SphereCollider.SetActive (true);
}
if (ringSevenTrigger == true)
{
ring7BoxCollider.SetActive (true);
ring7SphereCollider.SetActive (false);
}
else
{
ring7BoxCollider.SetActive (false);
ring7SphereCollider.SetActive (true);
}
if (ringEightTrigger == true)
{
ring8BoxCollider.SetActive (true);
ring8SphereCollider.SetActive (false);
}
else
{
ring8BoxCollider.SetActive (false);
ring8SphereCollider.SetActive (true);
}
if (ringNineTrigger == true)
{
ring9BoxCollider.SetActive (true);
ring9SphereCollider.SetActive (false);
}
else
{
ring9BoxCollider.SetActive (false);
ring9SphereCollider.SetActive (true);
}
if (ringTenTrigger == true)
{
ring10BoxCollider.SetActive (true);
ring10SphereCollider.SetActive (false);
}
else
{
ring10BoxCollider.SetActive (false);
ring10SphereCollider.SetActive (true);
}
}
}
What's the name of the GameObject you attached this script? And what's the name of the copy of your gameObject?
I strongly feel that when you find the GameObjects with those Tags you have, may be, because of the same name or tag, its always returning only one gameobject, and not the other, so only that gameobject is disabled everytime.
Answer by gamescorpion · Oct 19, 2014 at 03:08 AM
I solved this by simply removing my script from the object and RE-adding it back in through the inspector. Worked fine for me afterwards. Its like when you duplicate objects, it keeps them with their original settings like a prefab, so I have to always remove/reapply items manually. Ex. Take a plane, add a texture, press CTRL+D to duplicate. After if you try and change the texture via inspector, BOTH copies will change the same way like a prefab would, HOWEVER if you now drag and drop a new texture from Project Explorer to one of the planes, the texture will change correctly and both objects will act the way you want. The same method will hold true with scripts and other items as per my findings.
Hope this helps others (I'm sure this is not the "Best" method, but hey it works lol!).
Wow, just wow... I removed the script and added it again as you suggested and it miraculously works now. I spent one hour trying to figure out what was wrong... turns out nothing was. Thanks a lot, Unity! Can't believe this is an issue since 2014.
Answer by fox2402 · Feb 12, 2014 at 02:37 PM
Since you haven't post your code, i can't say a lot. But, if you want to make multiple object, try using a prefab and the Instantiate function, or else try to check your conditions inside your trigger. Can't say more without your code^^'
Answer by RedDevil · Feb 12, 2014 at 03:22 PM
When you disable objects this works.When you try to reenable objects this does not work since you disabled the object you disabled the script so now you cannot enable it again.
The script is not attached to those objects they enable and disable perfectly fine when I only use one trigger object , its when I try add the script to an identical object the trigger is recagnoised but it will not run the functions on the new trigger the origial trigger will continue to work
Ok so to solve this the script was fine , i added a second collider to the orignal trigger object and moved it into the position where I wanted the second and all is working fine now thanks guys