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 /
avatar image
0
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
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

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

How to Find all objects with Tag and List their Transforms? 4 Answers

Add GameObject to List using Timer 1 Answer

How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer

Panel GameObject not activating 0 Answers

Unity 3D: how to make the game object increase the speed on continually tapping? 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