- Home /
 
C# Script is affecting multiple GameObjects but I would like it to only affect one of them...
As a part of my game, there is a mechanic for falling platforms. When I first added a falling platform, I thought it would work, but I placed another one later on in the level and it turned out that when I triggered one of the falling platforms, it affected the other one.
Here are my scripts:
Trigger Script -
 using UnityEngine;
 using System.Collections;
 
 public class FallingScriptTrigger : MonoBehaviour {
 
     void OnTriggerEnter (Collider other)
     {
         if(other.tag == "Player")
         {
             fallingScript.PlayerIsOnTop = true;
         }
     }
 
     void OnTriggerExit (Collider other)
     {
         if(other.tag == "Player")
         {
             fallingScript.PlayerIsOnTop = false;
         }
     }
 }
 
 
               Controller Script -
 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody))]
 [AddComponentMenu("Platform/Falling Platform Script")]
 public class fallingScript : MonoBehaviour {
 
     //Sound Prefabs
     public GameObject IndicatorSound;
     public GameObject fallingSound;
 
     //Indication GameObjects
     public GameObject indicator1;
     public GameObject indicator2;
     public GameObject indicator3;
 
     //Variables
     public float intervalTime;
     public static bool PlayerIsOnTop = false;
     bool OneTime = true;
     Vector3 startPos;
     bool hasStartedFalling = false;
 
     // Use this for initialization
     void Start () {
         IndSetAct(false, false, false);
         rigidbody.constraints = RigidbodyConstraints.FreezeAll;
         startPos = transform.position;
     }
 
     void Update ()
     {
         if(PlayerIsOnTop == true)
         {
             if(OneTime == true)
             {
                 StartCoroutine("seq");
                 OneTime = false;
             }
         }
         else
         {
             if(hasStartedFalling == false)
             {
                 if(transform.position == startPos)
                     {
                         OneTime = true;
                         StartCoroutine("sseq");
                     }
             }
         }
     }
 
     void IndSetAct (bool setActiveBool1, bool setActiveBool2, bool setActiveBool3)
     {
         indicator1.SetActive(setActiveBool1);
         indicator2.SetActive(setActiveBool2);
         indicator3.SetActive(setActiveBool3);
     }
 
     IEnumerator seq ()
     {
         IndSetAct(true, false, false);
         Instantiate(IndicatorSound);
         yield return new WaitForSeconds(intervalTime);
         IndSetAct(true, true, false);
         Instantiate(IndicatorSound);
         yield return new WaitForSeconds(intervalTime);
         IndSetAct(true, true, true);
         Instantiate(IndicatorSound);
         yield return new WaitForSeconds(intervalTime);
         IndSetAct(false, false, false);
         Instantiate(fallingSound);
         rigidbody.constraints = RigidbodyConstraints.None;
         rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
         rigidbody.constraints = RigidbodyConstraints.FreezePositionX;
         rigidbody.constraints = RigidbodyConstraints.FreezePositionZ;
         hasStartedFalling = true;
         yield return null;
     }
     IEnumerator sseq ()
     {
         StopCoroutine("sseq");
         rigidbody.constraints = RigidbodyConstraints.FreezeAll;
         IndSetAct(false, false, false);
         transform.position = startPos;
         yield return null;
     }
 }
 
               Please help asap... I can't really progress the game's development without fixing this bug. Thanks...
Answer by flaviusxvii · Aug 04, 2014 at 03:05 AM
 public static bool PlayerIsOnTop = false;
 
               You should look up what static means. It's the problem. 
Its so simple. Im sorry, but when I uploaded this post, it was 1am and I was tired. Its so stupid how I didnt understand this... Oh well. What I did was I combined the two scripts so that the OnTriggerEnter/Exit was all controlled from the same place. I know I could have done it a more efficient way but, hey, what I did works. Thanks to flaviusxvii for re$$anonymous$$ding me. (I facepalmed so hard on this one.)
Your answer