• 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
Question by kellyb914 · Apr 04, 2016 at 08:25 AM · c#scripting problemgameobjectlist

Update list on mouse click

I am currently working on a 2d board game. My logic is as follows: A player will click on a card, then a gamepiece will flash where the player can move. When the player moves there, a list will be updated with the current position. What I'm having problems with is when I click on a piece, that individual piece with a script called MakeMove only get's updated, while all other pieces do not update.

Here is what I have:

 void OnMouseOver()
 {
     if (Input.GetMouseButtonDown(0) && gameObject.tag == "Card")
     {
         setCard(gameObject);
     }
     else if (cardName != null)
     {
         if (Input.GetMouseButtonDown(0) && gameObject.tag == "BlueHome")
         {
             if (hPos0.GetComponent<SpriteRenderer>().color.Equals(getColor("blue")) && IsStartEmpty())
             {
                 moveOutOfHome(0, 0, "blue", cardName);
                 isHomeEmpty = false;
             }
             else if (hPos1.GetComponent<SpriteRenderer>().color.Equals(getColor("blue")) && IsStartEmpty())
             {
                 moveOutOfHome(1, 0, "blue", cardName);
                 isHomeEmpty = false;
             }
             else if (hPos2.GetComponent<SpriteRenderer>().color.Equals(getColor("blue")) && IsStartEmpty())
             {
                 moveOutOfHome(2, 0, "blue", cardName);
                 isHomeEmpty = false;
             }
             else if (hPos3.GetComponent<SpriteRenderer>().color.Equals(getColor("blue")) && IsStartEmpty())
             {
                 moveOutOfHome(3, 0, "blue", cardName);
                 isHomeEmpty = false;
             }
             else
             {

             }
         }
         else if (Input.GetMouseButtonDown(0) && gameObject.tag == "PlaceHolder" && !(gameObject.GetComponent<SpriteRenderer>().color.Equals(getColor("white"))))
         {
             Debug.Log(cardName);
             makeMove(Int32.Parse(getCardAsString()), hColor);
         }
         else if (Input.GetMouseButtonDown(0))
         {
             Debug.Log(gameObject.tag.ToString());
         }
     }
 }
 
 
 public void moveOutOfHome(int initialPos, int finalPos, string color, GameObject card)
 {
     click = gameObject;
     card = cardName;
     GameObject initialP = homePositions[initialPos];
     initialP.GetComponent<placeHolderPrefab>();
     initialP.GetComponent<SpriteRenderer>().color = getColor("white");

     GameObject finalP = placeHolderPositions[finalPos];
     finalP.GetComponent<placeHolderPrefab>();
     finalP.GetComponent<SpriteRenderer>().color = getColor("blue");

     Destroy(cardName);
     setActivePositionsList();
 }
 
 public void setActivePositionsList()
 {
     placeHolderPositions = GameObject.FindGameObjectsWithTag("PlaceHolder");
     homePositions = GameObject.FindGameObjectsWithTag("BlueHome");
     
     for (int i = 0; i < placeHolderPositions.Length; i++)
     {
         if (placeHolderPositions[i].GetComponent<SpriteRenderer>().color.Equals(getColor("blue")))
         {
             activePositionsList.Add(i);
         }
     }
 }
Comment

People who like this

0 Show 0
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

1 Reply

  • Sort: 
avatar image

Answer by b1gry4n · Apr 04, 2016 at 11:13 AM

I am guessing you have this script on every game object. Each game object is not updating because when you are getting the "OnMouseOver" function, it is applied to that specific game object containing that instance of your script, not all of them. From there it is only updating itself because your functions are being called and applied only to that specific instance of the script. The individual game pieces have no idea that the others even exist

It would be easier to create a separate manager type script that handles updating all the pieces. If these objects arent instantiated at run time you could drag/drop them all into a list. If they are, just add them to the list when they are created. Your manager script would contain a list of all active game pieces that you could do whatever you want with.

 List<MakeMove> gamePieces;

Instead of having each game piece try and update everyone else, just have that game piece call to the "manager" script and tell it to update everyone. Call it when youre ready to update its position and everyone elses.

 public void ReadyToUpdate(){
 managerScript.UpdatePositions();
 }

In your manager have a function that gathers whatever information you need from the "gamePieces" list and store their positions.

     public void UpdatePositions()
     {
         for (int x = 0; x < gamePieces.Count; x++)
         {
            //do whatever to each gamePieces[x]
         }        
     }
Comment
braden.kelly

People who like this

1 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Clear Does not work to remove 1 Answer

How to remove an item from a list of custom variables 1 Answer

Get first gameobject in a list and cycle through on keypress 1 Answer

How to detect an object which be in FOV of certain camera ? 1 Answer

How to add elements from a List of another class to another List (C#) 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