- Home /
Is there a way to change the sprite for all instances of the same object simultaneously?
I need to make it so when you hover the mouse over one object all of the other prefabs change sprites as well. I also need it to do this on click. Here is the code I have currently. It still only changes sprites for the object selected.
using UnityEngine; using System.Collections; public class poisongrass : MonoBehaviour { public Sprite[]scanSprite; public bool isClicked = false; public GameObject[]allGo; public int iGo; static GameObject instance =null; //Causes the player to take a constant damage of .25 as long as they are in this objects trigger collider area.
void Update(){
allGo = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];
}
public void OnTriggerStay2D(Collider2D trigger){ GameObject.Find ("character").GetComponent().health = GameObject.Find ("character").GetComponent().health - .25;
}
public void clicked(){
if(allGo[iGo].tag== "pgrass"){
print("Vile Weed only grows in dark areas. Contact will cause severe damage due to its acidic properties.");
if(isClicked== false){ this.GetComponent().sprite=scanSprite[1]; isClicked=true; } } }
public void mouseOn(){
if(allGo[iGo].tag== "pgrass"){ if(isClicked == false){ this.GetComponent().sprite=scanSprite[0]; }else if(isClicked ==true){
this.GetComponent().sprite=scanSprite[1]; } }
}
public void mouseOff(){
this.GetComponent().sprite=scanSprite[2]; }
}
Answer by arnatus · Sep 25, 2017 at 04:00 AM
not sure what you're looking for but if I'm not wrong you want to change sprite of all GameObjects? allGo?
you should consider using a loop either "for" or "foreach"
foreach (GameObject go in allGo) {
go.GetComponent<SpriteRenderer>().sprite = scanSprite[1];
}
also you can write the if condition like that
if(!isClicked) {} // use ! to check if it's false
if(isClicked) {} // check if it's true
Ok thanks, so putting them in a loop like that will make every object with the tag pgrass change sprites at the same time? Basically pgrass means poisonous grass, but I have like 20 of them in the scene. I have it set up where when you hover the mouse over the grass it highlights blue and when you click on the object it tells you a little bit of information(only in the console at the moment). Since all 20 of them are the same object I want it to be set up so that when you hover over 1 of the poisongrass objects all of the others highlight as well. @arnatus
I see what you want to do there. So basically each GameObject will work independently from any other GameObject even if they have the same gameObject.tag and a On$$anonymous$$ouseOver() method will affect only itself. And to make things more clear your tag will just identify the GameObjects in your scene but to access them you are going to use a loop in the Update() to look for GameObjects with the "pgrass" each frame (that's if your game is so dynamic and pgrass can be destroyed at some point with the player interactions) otherwise any Unity method would work like Start() or On$$anonymous$$ouseOver() for instance.
void Start() {
allGo = GameObject.FindGameObjectsWithTag("pgrass");
// do a loop here if you want to add (this script) as a Component to each GameObject of allGo dynamically when game starts.
foreach(GameObject pgrass in allGo) {
pgrass.AddComponent<ThisScriptName>();
}
}
void On$$anonymous$$ouseOver() {
for (int i = 0; i < allGo.length; i++) {
// your code to highlight all the pgrass goes here
allGo[i].GetComponent<SpriteRenderer>().sprite = scanSprite[1];
if(Input.Get$$anonymous$$ouseButtonDown(0)) {
// things to do if this GameObject was clicked goes here
}
}
}
Finally there is no way you could make GameObjects work as one unless you loop through them and change them one by one. Besides that, don't forget to use colliders on your prefabs and also don't forget to change back the sprite to its default value after mouse leaves the GameObject. PS: you can use On$$anonymous$$ouseExit() I hope that helps and you can make all your pgrass highlight!
I forgot to mention that On$$anonymous$$ouseOver() is called every time and every frame when the mouse is over the GameObject so all the code within it will be called repeatedly until the mouse leaves (exit) and making a console log here may flood your console with the same log multiple times so take that into consideration and/or use a bool to prevent it from flooding console.
Your answer
Follow this Question
Related Questions
Stretching certain part of a sprite (like ui) 1 Answer
Prefabs and multiple tilesets? 0 Answers
Problem with a color randomization 2 Answers
Texturing custom 2d sprite 0 Answers