• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FrancisBrunel · Nov 26, 2021 at 10:56 AM · variablevariablesreferencevaluesreferences

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Nov 26, 2021 at 02:14 PM 0
Share

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?

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error message "variable not assigned" ? 1 Answer

value of another script not being read? 0 Answers

How do I make a list of references to a variable? 2 Answers

Changing a public varible from another script 3 Answers

Having a reference to only one variable. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges