- Home /
How to change a value of a reference object variable after a while ?
Hello everyone,
After some research I can not solve my problem.
In the (simplified) example below I change the boolean values "callSignIsGiven" and "aircraftTypeIsGiven". It works perfectly fine but the changes are simultaneous to the execution of "Indication()".
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public string trafic;
// Update is called once per frame
void Update()
{
//some code checking if "Indication()" has to be executed
}
public void Indication()
{
if (planeIdentificationCase1)
{
trafic = "RedPlane";
}
else if (planeIdentificationCase2)
{
trafic = "BluePlane";
}
else if(planeIdentificationCase3)
{
trafic = "GreenPlane";
}
//an example of an indications
if (informationIndication1)
{
GameObject.Find(trafic).GetComponent<Plane>().callSignIsGiven = true;
}
if (informationIndication2)
{
GameObject.Find(trafic).GetComponent<Plane>().aircraftTypeIsGiven = true;
}
//etc...
}
}
So, I find a solution on the Internet to add some delay between the execution and the changes. Here is the new version of the code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public string trafic;
// Update is called once per frame
void Update()
{
//some code checking if "Indication()" has to be executed
}
public void Indication()
{
if (planeIdentificationCase1)
{
trafic = "RedPlane";
}
else if (planeIdentificationCase2)
{
trafic = "BluePlane";
}
else if(planeIdentificationCase3)
{
trafic = "GreenPlane";
}
//an example of an indications
if (informationIndication1)
{
StartCoroutine(WaitBeforeCorrection(a => GameObject.Find(traficConcerne).GetComponent<Plane>().callSignIsGiven = a));
}
if (informationIndication2)
{
StartCoroutine(WaitBeforeCorrection(a => GameObject.Find(traficConcerne).GetComponent<Plane>().aircraftTypeIsGiven = a));
}
//etc...
}
IEnumerator WaitBeforeCorrection(System.Action<bool> aCallback)
{
yield return new WaitForSeconds(20f);
aCallback(true);
}
}
Unfortunately, if "Indication()" is executed again before the 20 seconds, the changes will apply to another aircraft, let's say the "BluePlane" instead of the "RedPlane". It's because the variable "trafic" changes each time "Indication()" is called.
I tried to fix it by changing "trafic" to "trafic[]", in order to store all the previous values of "trafic" in a List so I am able to apply the boolean changes to a variable that will not change anymore. But doing that, I get somme weird "Out of bounds" error. I checked and "Indication()" execute only once in a while. The error appears even at the first line for "trafic[0]=...".
Do you have any suggestions ?
I tried to make it as simple as possible, but ask me for any detail you would need.
Thank you in advance for the time dedicated to my issue,
Francis
I dont understand your issue, the problem is that everytime you call Indication method the airplane changes? then why not simply not change the airplane? or you want the airplane not to you during the 20 seconds, and then be able to change again?
The issue is that the method is called frequently and randomly in fact, which leads to trouble to follow wich airplane is concerned by boolean changes.
I could write a method for every airplane available, but it means dozens of equivalent methods. It is a nonsense to me.
"which leads to trouble to follow wich airplane is concerned by boolean changes"
But why do you need to keep track of which airplane is concerned by boolean changes? could you explain what happens, and what it is suppose to happeb?