Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Brokenarrow · Mar 08, 2016 at 03:56 PM · getcomponentgameobjectslistsforeachnew

Problem with reading values from scripts on GameObjects in Lists

Hi, I am having a problem with reading values from scripts on GameObjects which are stored in Lists. What I am trying to accomplish is the following:

The game I'm making is a small turn based strategy-game in which you conquer territory. It revolves around the GameAction GameObject. This contains a script which has some values, most importantly a selectedTerritory GameObject and a targetTerritory GameObject.

During a player turn, a player can select a friendly territory and rightclick an enemy territory. This triggers the creation of a new GameAction GameObject:

 newAction = gameAction;
 newAction.GetComponent<GameAction>().currentAction = GameAction.typeOfAction.Attack;
 newAction.GetComponent<GameAction>().selectedTerritory = selectedTerritory;
 newAction.GetComponent<GameAction>().targetTerritory = targetTerritory;
 gameActionList.Add(newAction);

Then, at the end of the turn, I want to read all the information from all the stored GameAction GameObjects.

 public void ResolveGameActions() 
     {
         gameActionsToBeResolved = mapManager.GetComponent<MapManager>().gameActionList;
 
         foreach(GameObject gameAction in gameActionsToBeResolved)
         {
             Debug.Log(gameAction.GetComponent<GameAction>().targetTerritory.name);
         }
         
     }

I expected this method to return all names of targetTerritories the player selected during play. However it only returns the name of the last selected targetTerritory 5 times. To me it seems that all GameActions im creating are the same: but I'm not sure how to do it otherwise. Any help would be greatly appreciated.

Comment
Add comment · Show 1
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 TheNoobieWaffle · Mar 08, 2016 at 04:12 PM 0
Share

It always returns the same thing 5 times no matter if the player selects ANY number of things, or returns the last one 5 times if the player selects 5 things? EDIT: Are you sure that your selectedTerritory variable in the $$anonymous$$ap$$anonymous$$anager is being altered when the player tries to select something?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by NoseKills · Mar 08, 2016 at 04:27 PM

Doing this newAction = gameAction; doesn't create anything new. It just assigns a reference pointing to "gameAction" object into a new variable.

If this truly is all the relevant code and you are not creating new instances of GameAction class anywhere, then you are just putting the same object in the list over and over and changing the values in this one object.

Since GameAction is a MonoBehaviour, to create a new instance of GameAction you would have to do something like

 newAction = new GameObject("action").AddComponent<GameAction>;
 ...

If it was a normal class, you'd use the new keyword and call the constructor to directly make new instances.

 newAction = new GameAction();
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 Brokenarrow · Mar 08, 2016 at 05:34 PM 1
Share

Thanks for the answer!

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

37 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

Related Questions

Trying to get gameobject from one list to follow gameobjects in another list 1 Answer

How would I access a property of another objects child component? 2 Answers

How to get variable from a 2D list in c#? 1 Answer

How to deal with a missing reference when an object referred each frame from an array is destroyed along the way? 2 Answers

Trying to program two buttons to appear when the player in my game dies 0 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