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 cjjacobs97 · Sep 25, 2017 at 03:32 AM · c#spriteprefabsspriterendererinstances

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]; }

}

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
0

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


Comment
Add comment · Show 3 · 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 cjjacobs97 · Sep 25, 2017 at 05:54 AM 0
Share

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

avatar image arnatus cjjacobs97 · Sep 29, 2017 at 03:45 AM 0
Share

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!

avatar image arnatus arnatus · Sep 29, 2017 at 04:01 AM 0
Share

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

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

402 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Problems changing sprite with code through multiple scripts please help!!,Problems linking C# code to Sprite Renderer 0 Answers

Texturing custom 2d sprite 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