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
1
Question by ronkrepps · May 28, 2018 at 06:36 AM · prefabspriteclone

How to change sprite on a cloned object?

Update 2: I found out that the Animator Controller on the object is "overriding" the sprite. If I disable the animator the correct sprite shows. But then I can't play the animations when they are required. Any ideas???

Update: Check this image library post showing what the sprite is assigned to and how it reverts back: https://imgur.com/a/Fd7RXwd

So I am attempting to change a sprite in the start function of a cloned object. I'm using the code below and when I set the sprite its set to the new sprite, but it's constantly reverting the sprite back to the sprite associated with the prefab object sometime after that. Anyone have an idea how this is possible or what I am doing wrong?

     private SpriteRenderer spriteRenderer;
 
     public Sprite break1;
     public Sprite break2;
     public Sprite break3;
     
     void Start ()
     {
         spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
 
         switch (Type)
         {
             case DropType.Breakable1:
                 spriteRenderer.sprite = break1;
                 break;
             case DropType.Breakable2:
                 spriteRenderer.sprite = break2;
                 break;
             case DropType.Breakable3:
                 spriteRenderer.sprite = break3;
                 break;
         }
     }

I tried an alternate method by setting the sprite directly after instantiating it and this code does not work either:

 switch (type)
         {
             case DropType.Blue:
                 cs.Drop = Instantiate(Drop1, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
                 break;
             case DropType.Orange:
                 cs.Drop = Instantiate(Drop2, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
                 break;
             case DropType.Green:
                 cs.Drop = Instantiate(Drop3, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
                 break;
             case DropType.Red:
                 cs.Drop = Instantiate(Drop4, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
                 break;
             case DropType.Breakable1:
                 cs.Drop = Instantiate(BreakableDrop, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
                 break;
             case DropType.Breakable2:
                 cs.Drop = Instantiate(BreakableDrop, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
                 break;
             case DropType.Breakable3:
                 cs.Drop = Instantiate(BreakableDrop, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
                 break;
             case DropType.Star:
                 cs.Drop = Instantiate(Star, new Vector3(cs.transform.position.x, 10.0f), Quaternion.identity);
                 break;
         }
         
         cs.State = CellScript.CellState.Filling;
                
         var ds = cs.Drop.GetComponent<DropScript>();
         ds.Type = type;
         ds.cellScript = cs;
         ds.Fall(ds.transform.position, cs);
 
         var spriteRenderer = cs.Drop.GetComponent<SpriteRenderer>();
 
         switch (type)
         {
             case DropType.Breakable1:
                 spriteRenderer.sprite = break1;
                 break;
             case DropType.Breakable2:
                 spriteRenderer.sprite = break2;
                 break;
             case DropType.Breakable3:
                 spriteRenderer.sprite = break3;
                 break;
         }

I can see through debugger that sprite is changing from the default prefab, to the new one I am setting. But then it always reverts sometime after this back to what the prefab default.

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 tormentoarmagedoom · May 26, 2018 at 09:00 AM

Good day.

Maybe the best way to do is by changing it when you instantiate the object. You can define a GameObjecrt variable, to instantiate an object, so you can modify everything you need from that instantiated object, like any other. Something like this:

 GameObject ObjectInstantiated = Instantiate (prefab, position, rotation) as GameObject;

Now The instantiated object is stored in "ObjectInstantiated" variable, so you can edit it as any other obejct in the scene:

 ObjectInstantiated.transform.position = NewPosition;
 ObjectInstantiated.transform.parent = SomeOtherTransform;
 ObjectInstantiated GetComponent<SpriteRenderer>().sprite = break1;

Is a good way to control what and how are you spawning things.

bye!! :D

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 ronkrepps · May 26, 2018 at 06:16 PM 1
Share

Hi thanks for your comment. This is basically what I am doing except I had the sprite changing code in the script for the gameobject because I use the game object in multiple other scripts. I tried moving it to the same script I am instantiating the object and it is still not working. It goes back directly to the prefab object sprite.

avatar image Aggrojag ronkrepps · May 26, 2018 at 07:00 PM 0
Share

Is there any other related code? I'd do a search for "sprite" in all code that touches the instantiated object. Then you can make sure some old bit of code, or a mistake, isn't causing the issue.

avatar image ronkrepps Aggrojag · May 26, 2018 at 08:33 PM 0
Share

Hi I thought of that too so I did a search and I'm not accessing the spriterenderer of this object anywhere else in code. I will reply with all relevant code but there really isnt much else.

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

132 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

Related Questions

Overlapping objects increase drawcalls in 2DToolkit 0 Answers

Get all sprites of the prefab in order and change their sprite 1 Answer

Assign an instantiated GameObject? 1 Answer

FLAPPY BIRD CLONE 1 Answer

Loosing sprite association when transferring prefab 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