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 /
avatar image
0
Question by Worempie · Dec 15, 2014 at 05:50 PM · gameobjectlistswitch characters

Switching from GameObject to GameObject by using a list

Hello,

I've tried around with Arrays and Lists for a while and got one working. I finally managed to make a list which orders the values of every single GameObject from high to low value. The problem now is that when I click on next turn. He doesn't add +1 on the list number. The log keeps on updating with 0, 1, 2, 3, 0, 1, 2... etc. It does switch the player but to the number 2 in the list (so actually gameobject 3). How do I fix this? Here is the code:

public class TurnBasedStates : MonoBehaviour {

 private CharacterStats statsmanager;
 public List<GameObject> characterList = new List<GameObject>();
 private GameObject currentCharacter;

 public enum BattleStates{
     START,
     ACTIONPHASE,
     ANIMATION,
     ENDTURN,
     LOSE,
     WIN,
     NEXT
 }

 private BattleStates currentState;

 // Use this for initialization
 void Start () {
     GameObject[] allCharacters = GameObject.FindGameObjectsWithTag ("Character");
     allCharacters = GameObject.FindGameObjectsWithTag("Character").OrderBy(go => go.GetComponent<CharacterStats>().initiative).ToArray();
     System.Array.Reverse (allCharacters);
         foreach (GameObject character in allCharacters) 
     {
         characterList.Add (character);
     }
  }
 


 // Update is called once per frame
 void Update () {
             //Debug.Log (currentState);
             switch (currentState) {
             case(BattleStates.START):
         currentCharacter = characterList[0];
         currentCharacter.GetComponent<CharacterStats>().isActive = true;
             break;
             case(BattleStates.ACTIONPHASE):
                     break;
             case(BattleStates.ANIMATION):
                     break;
             case(BattleStates.ENDTURN):
                     break;
             case(BattleStates.LOSE):
                     break;
             case(BattleStates.WIN):
                     break;
             case(BattleStates.NEXT):
             currentCharacter.GetComponent<CharacterStats>().isActive = false;

             for (int i = 0; i < characterList.Count; ++i)
             {
                 currentCharacter = characterList[i];
                 currentCharacter.GetComponent<CharacterStats>().isActive = true;
                 Debug.Log(i);
                 currentState = BattleStates.ACTIONPHASE;
             }

                     //statsmanager = GameObject.Find("Character").GetComponent<CharacterStats>().initiative;
                     //currentState = BattleStates.ACTIONPHASE;
             break;
                     
             }
     }

 void OnGUI(){
         if(GUILayout.Button("END TURN")){
                 currentState = BattleStates.NEXT;
             }
     }

}

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 SkaredCreations · Dec 15, 2014 at 05:53 PM 0
Share

What does it mean "+1"? If you don't call characterList.Add(anotherGameObject) then it cannot add anything and the list remains the same.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KarlGG · Dec 15, 2014 at 08:13 PM

I think you're tripping yourself up using loops in the Update function.

For example, when you enter the "NEXT" case of the switch, the first thing you do is set currentCharacter to not active... But then loop through every single character turning every single one back on. You print each number to the console, and set the currentState to "ACTIVE" repeatedly. You have changed the state, but it has not left the loop inside the Update yet, so it will not trigger a new case until the next frame after the loop has gone through every single one.

Instead of looping, just increment a pointer (once), check for wrap around, and leave the case.

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 Worempie · Dec 16, 2014 at 11:12 AM 0
Share

You are right. Ins$$anonymous$$d I used this. It took me quite a long time to find this, but it seems to select the gameobjects one by one and once at the end of the list it will go to the first one again. Thanks for the advice :)

case(BattleStates.NEXT): currentCharacter.GetComponent().isActive = false;

             int index = characterList.IndexOf(currentCharacter);
             if(index < characterList.Count - 1)
             {
                 index++;
             }
             else
             {
                 index = 0;
             }
             currentCharacter = characterList[index];
             currentCharacter.GetComponent<CharacterStats>().isActive = true;
             Debug.Log(index);
             currentState = BattleStates.ACTIONPHASE;
                     //statsmanager = GameObject.Find("Character").GetComponent<CharacterStats>().initiative;
                     //currentState = BattleStates.ACTIONPHASE;
             break;
                     
             }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

use list of gameobjects transform in raycast 0 Answers

When I add a gameObject from one list to another, it makes a new one instead of referring to the same one? 0 Answers

Enabled all gameobjects in array 1 Answer

Getting a List of Gameobjects in a collider 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