- Home /
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);
}
}
}
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]
}
}
Your answer
Follow this Question
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