Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • 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
-1
Question by ShadyProductions · Jul 31, 2015 at 11:54 PM · variablestringstructother scriptmultiple instaces

How to get a value and keep it updated

I'm trying to get the hunger value from anotherscript etc and it works, but as it is updated in its script, i can't update it in the current script, since the value is only called once I need some help to work around this:

     private AIbehaviour AIb;
     private int amountOfWorkers;
     private List<int> worker = new List<int>();
 
     public struct WorkersInfo //had no better idea lol
     {
         public float hunger; 
         public float sanity;
         public float tiredness;
         public bool workBoost;
         public string Name;
     }
 
     private WorkersInfo[] wi = new WorkersInfo[50]; //50 just incase
     
     [Multiline]
     private string TextObj = "Workers Status's: \n";
 
     void updateText() {
         if (worker.Count == 0) {
             worker.Clear();
         } else { //grab value from 0 index and remove it, until update is called again
             TextObj += "\n" + wi[worker[0]].Name + ": \n" //with a new value ^
                 + "- Hunger: " + wi[worker[0]].hunger+ "\n" //updated once here
                 + "- Sanity: " + wi[worker[0]].sanity + "\n"
                 + "- Tiredness: " + wi[worker[0]].tiredness + "\n"
                 + "- Workboost Enabled: " + wi[worker[0]].workBoost + "\n";;
             worker.RemoveAt(0); //removed from list again to prevent copying
         }
     }
 
     void getWorkersInfo() {
         foreach (Transform child in workerList) {
             AIb = child.GetComponent<AIbehaviour>();
             if (AIb.available && !AIb.isCountedIn) { //look for every worker
                 amountOfWorkers += 1;
                 worker.Add(amountOfWorkers); //add to list
                 AIb.isCountedIn = true; //become unfindable to stop duplication
                 wi[amountOfWorkers].hunger = AIb.hunger; //save the value to var
                 wi[amountOfWorkers].sanity = AIb.sanity;
                 wi[amountOfWorkers].tiredness = AIb.tiredness;
                 wi[amountOfWorkers].Name = AIb.Name;
                 wi[amountOfWorkers].workBoost = AIb.workBoost;
                 updateText(); //call update for every worker found
             }
         }
     }

//after all workers are found and correctly added, there is no way of updating its properties any ideas? I also figured that i'd probably have to completely replace the updateText since it will just add new ones, and you can't really update the text's variables only, any ideas people?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Aug 01, 2015 at 09:00 AM

Use an event system. Turn your hunger variable into a property that calls the event as well. The other class is registered as listener.

 public class HungerClass:MonoBehaviour{
     public Action<float> OnHungerChange = ()=>{ };
 
     private float hunger = 0f;
     public float Hunger{ // May not need to be public
          get{return hunger;}
          set{
               hunger = value; 
               OnHungerChange(hunger);}
     }
     private void OnDestroy(){OnHungerChange = null;}  
 }
 
 public class HungerListener:MonoBehaviour{
     private HungerClass hungerClass = null;
     private float hunger = 0f;
     void Awake(){
          hungerClass = GetComponent<HungerClass>(); // May require to find the object first
          if(hungerClass != null){
              hungerClass.OnHungerChange += this.OnHungerChangeListener;
          }
     }
     private void OnDestroy(){
         if(hungerClass != null){
              hungerClass.OnHungerChange -= this.OnHungerChangeListener;
              hungerClass = null;
          }
     }
     private void OnHungerClassListener(float newValue){
         hunger = newValue;
     }
 }

So when the HungerClass uses the Hunger property, the variable is updated and the event is called. Any listeners will get the call and will perform whatever action is meant to. This way, no need for checking the original hunger value. Just wait an listen.

The OnDestroy's are here to make sure that if the object is destroyed, it is removed as listener and avoid a null reference exception.

Comment
Add comment · Show 4 · Share
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 getyour411 · Aug 01, 2015 at 09:32 AM 0
Share

Nice @fafase very reusable too, thanks for sharing.

avatar image ShadyProductions · Aug 01, 2015 at 10:23 AM 0
Share

can this be used for multiple instances of hunger? since there is more than one AI

avatar image fafase · Aug 01, 2015 at 11:13 AM 0
Share

yes. the event is not static so each HungerClass contains its own event. So if you place both components on one AI then GetComponent will find the HungerClass on that object and will not know about HungerClass of other objects.

avatar image getyour411 · Aug 02, 2015 at 04:30 AM 0
Share

@fafase In the months since I've been learning Unity (+ new to C#/dev in general), I've learned so much from reading examples like yours above and have gone from completely clueless to kinda getting it, and it's been a fun journey (that I'm still on!).

Events, delegates, LINQ, Patterns and other Intermediate concepts (and I wince at that wondering what Advanced topics have in store) can be sorta baffling to code 101ers. I find the Unity tutorial sequences to be fantastic and it's really awesome that these are made available for free.

https://unity3d.com/learn/tutorials/topics/scripting

avatar image
-1

Answer by getyour411 · Aug 01, 2015 at 12:03 AM

 void Update() {
 
 // do stuff
 
 }

Update is called every frame. See also Invoke, InvokeRepeating, Coroutine

Comment
Add comment · Show 1 · Share
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 getyour411 · Aug 01, 2015 at 08:49 AM 0
Share

None of what you've said, in the problem description or your reply makes much sense; everything you've said 'there's no way to' and 'you cant update'...you can.

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.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Variable in String 1 Answer

GameObject to Variable to GameObject and add string? 1 Answer

Input Field Text string to string variable. (const string) 0 Answers

Get variable from other GameObject's script 3 Answers

variables as GUI 1 Answer


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