Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by ardy_asteria_sp · Aug 18, 2021 at 02:36 PM · variableseventscore systempointsinstances

How to access a variable from multiple instances and add them together

Hello.

I have many instances of a game object each with a variable that represents the points scored. When these game objects enter an area, they are added to a list. Once they're on that list, I'd like to be able to access each instance of the gameobject's points variable. And then add them together in a single variable.

For example, if I have 6 instances of the game object in the scene, but I then place 3 instances on the list, I'd like to the points variable on each of those 3 instances to add into a single total score variable. If the points on one instance changes, then I’d like the total score variable to automatically add the new point. If I take one instance off the list, and add a new instance to the list, I'd like the new instance’s points variable to start contributing to the overall score.

Below is where I have got to. Any help you can offer would be greatly appreciated

Script 1: A list to hold instances of the creature game object

    public class DungeonVisual : MonoBehaviour
     {
     public List<GameObject> CreaturesInDungeon = new List<GameObject>();
 // plus more code to add and remove creatures to dungeon etc. All that works fine so not copied here
     }

Script 2: CreatureManager

CreatureManager script attached to a prefab GameObject called creature. Game starts with creatures instantiating and there are multiple instances of Creatures in the scene. All instances start the scene not on the dungeon list.

Each creature has a variable called creaturePoints. This is a variable is the points acquired by each creature. The creaturePoints is itself derived from a csv file (for test purposes simulating real life point entries) that reads each time a particular instance of the game object gets a new creaturePoints. That part of the script works fine but is not included here. It works by going through the csv file in an IEnumertor method that reads off a new row every second and gets the respective creature’s points for that row.

I've set up an event so that every time a creaturePoints changes it will trigger an event in script 3.

     public class CreatureManager : MonoBehaviour 
     {
     private int creaturePoints = 0;
         public int CreaturePoints
         {
             get 
             { 
                 return creaturePoints;        
             }
             set
             {
                 if (creaturePoints == value) return;
                 creaturePoints = value;
     
                 if (OnPointsChange != null)
                     OnPointsChange(creaturePoints);
             }
         }
 
 
     public delegate void OnPointsChangeDelegate(int newVal);
     public event OnPointsChangeDelegate OnPointsChange;
 
 
 //plus all the other code for the variables and methods associated with creatures
 }

Script 3 ScoreManager The aim of this script is so that when creatures are in the dungeon, then the points they acquire while in the dungeon are added to the total score (scoreSum).

My problem is accessing all of the instances of the creature game object in the dungeon list, and adding each creature’s points into a single variable. I'd like it to be so that if just one creature gets a new point then only that point is added to the total.

The bit I’m stuck on #is simply adding each creature’s points to the total when they change in the GetCreatureScore() method.

I think this is because I need to access each instance of CreatureManager and the OnPointsChange variable. But I just can't figure out how to do it. I want it to work automatically when creatures enter the dungeon, but for testing, I'm using a KeyDown method to start it.

I've been working on getting this points and score problem right for literally months.

 public class ScoreManager : MonoBehaviour
 {
     public DungeonVisual dungeonVisual;
 
     public int scoreSum = 0;
     public int scoreToDisplay;
     public Text scoreText;
 
 void Update()
     {
         if (Input.GetKeyDown("s"))
 
             if (dungeonVisual.CreaturesInDungeon != null)
             {
                 GetCreatureScore();
             }
 
     }
 
     public void GetCreatureScore()
     {
         GetComponent<CreatureManager>().OnPointsChange += PointsChangeHandler;
     }
 
     private void PointsChangeHandler(int newVal)
     {
         if (dungeonVisual.CreaturesInDungeon != null)
 
             foreach (var creature dungeonVisual.CreaturesInDungeon)
             {
                 
                 scoreDisplay = creature.GetComponent<CreatureManager>().CreaturePointsSum;
                 scoreSum += scoreDisplay;
                 scoreText.text = string.Format("{000}", scoreSum);
             }
     }
 }

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

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.

Follow this Question

Answers Answers and Comments

170 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 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

How do I add extra points for coming close to obstacle but NOT for coming close and colliding? 1 Answer

Help With Score C# Error CS0131 1 Answer

Keeping points tallied 1 Answer

Coin Pickup system carry int into void 1 Answer

When Key is Pressed add one to variable 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