Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 LunaStudios · Apr 22, 2014 at 09:54 PM · c#instantiatesprite

How can I set a sprite to a bunch of clones?

Hi, again. Here another question. I got 6 clones each clone has 2 gameObject with a spriteRender. And now I want to change my sprites. But it changes only 1 clone and only 1 sprite. Maybe someone can help me.

     public Sprite[] SpritePipes;
     
     void Start() {
     Sprite mySpritePipes = SpritePipes[0];
     
                 for(float x = 0; x< 7.8f; x = x + 1.3f)
                 {
                 Instantiate(pipesStructur, new Vector3(x + 3, 0.9f, 0), Quaternion.identity);
     
                 test = GameObject.Find("Pipes(Clone)").GetComponentInChildren<SpriteRenderer>();
                 Debug.Log (GameObject.Find("Pipes(Clone)"));
                 test.sprite = mySpritePipes;
                 }
     }
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
Best Answer

Answer by xortrox · Apr 22, 2014 at 10:24 PM

I would place all instantiated pipes in a List or Array for future access, or maybe just their SpriteRenderer (so that you don't waste performance on GetcomponentInChildren)

Comment
Add comment · Show 5 · 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 LunaStudios · Apr 23, 2014 at 12:14 AM 0
Share

Thank you for your comment, but I dont see how it affects the result

avatar image xortrox · Apr 23, 2014 at 01:09 AM 0
Share

Here you are only affecting the sprite of one object because GameObject.Find("Pipes(Clone)") most likely finds the first instance of Pipes(Clone), you could however do GameObject g = (GameObject)Instantiate(pipesStructur, new Vector3(x + 3, 0.9f, 0), Quaternion.identity);

g.sprite = mySpritePipes;

So that you get the proper reference.

avatar image LunaStudios · Apr 23, 2014 at 09:37 AM 0
Share

50 % success! Now 6 sprites are right. my new script:

         for(float x = 0; x< 7.8f; x = x + 1.3f)
         {
         Instantiate(pipesStructur, new Vector3(x + 3, 0.9f, 0), Quaternion.identity);
         }

  

GameObject g = (GameObject)Instantiate(pipesStructur, new Vector3(x + 3, 0.9f, 0), Quaternion.identity);) doesnt work!

 But this:
     GameObject[] pipes = GameObject.FindGameObjectsWithTag("Pipe");
 
     foreach(GameObject pipe in pipes)
     {
         pipe.GetComponentInChildren<SpriteRenderer>().sprite = mySpritePipes;
     }

How is it possible that all sprites are changed?

avatar image LunaStudios · Apr 23, 2014 at 09:44 AM 0
Share

haha Im stupid. It works now! well I changed my childs Tags "Scene_Pipe" and now its done! ("Pipe" is already in use)

GameObject[] pipes = GameObject.FindGameObjectsWithTag("Scene_Pipe"); ...

thanks for helping me!

avatar image xortrox · Apr 23, 2014 at 02:43 PM 0
Share

Don't feel stupid, it's common to make mistakes, no problem.

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

21 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

Related Questions

Problem with Line Renderer 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Instantiated Sprite is not rendering. 1 Answer

Instantiating multiple sprites and assigning different colors for each 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