Hi. This is my 2nd project, and I have encountered a problem I did not expect.
I have a bunch of similar GameObjects
, which is why I copied them to get stuff done faster. From there, I changed scripts and names on each object, so that they would not be the same. Yet, when testing, they all are affected at the same time, even when only one of the objects is supposed to be affected.
Let’s say I have a public bool activate;
in each script, script1, script2, and script3. Each of the scripts is attached to their respective object, object1, object2, object3. The 2 last objects are copied from the first, the original script removed, and the respective scripts attached, as well as the original object name, changed. Using a Coroutine
, I change the bool activate
on object1 to true. The bool of the others is also changed, although I have not referenced them in the coroutine, only the bool from object1. I wonder what could make this happen.
For reference, this is the code I use;
Button to activate doors;
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonAction1 : MonoBehaviour
{
public GameObject door1;
public GameObject door2;
public float doorTime = 1;`
void Start()
{
gameObject.layer = 3;
}
void OnTriggerStay(Collider other)
{
StartCoroutine(CallDoor());
}
IEnumerator CallDoor()
{
yield return new WaitForSeconds(10);
door1.GetComponent<LockedDoors1>().activate1 = true;
door2.GetComponent<LockedDoors1>().activate1 = true;
}
}
Doors that sink in the ground, and rise again after the button is activated;
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LockedDoors1 : MonoBehaviour
{
Rigidbody rb;
public float sink = -5f;
public bool activate1;
float timePassed = 0;`
void Start()
{
gameObject.layer = 8;
rb = GetComponent<Rigidbody>();
Physics.IgnoreLayerCollision(0, 8);
activate1 = false;
}
public void Update()
{
if (activate1 == true)
{
StartCoroutine(Loop());
}
}
public IEnumerator Loop()
{
yield return new WaitForSeconds(1);
while (timePassed < 10)
{
rb.AddForce(0, sink, 0);
timePassed += Time.deltaTime;
yield return null;
}
rb.AddForce(0, 50, 0);
yield return new WaitForSeconds(10);
timePassed -= 10;
yield return null;
}
}
The values are not the same for each script, numbers are changed to differentiate (example is activate1, activate2, activate3 or LockedDoors1, LockedDoors2, LockedDoors3, etc.
Button object enters trigger collider, which activates the ButtonAction1 script
The doors with the LockedDoors1 script, there are 3 of these double doors, and each has their boolean activated by one single button, and not buy one respective button by each, which makes all 3 pairs open and close repeatedly, and not each pair individually.