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 /
This question was closed May 19, 2017 at 05:24 PM by sharahamo for the following reason:

Other

avatar image
0
Question by sharahamo · May 19, 2017 at 12:29 PM · prefabruntimespriterendererruntime-generation

how to change the sprite of instantiated prefabs

I would like to change the sprite during the runtime of a prefab. The sprite renderer is located in a child of the Prefabs 'starC'. Here's what I have done basically:

 void Awake() {
 
             starC.transform.parent = transform;
             starC.GetComponent<Egg> ().manager = this;
             starC.SetActive (false);
             starC.name = "starC";
 
             Sprite realEgg = Resources.Load("Egg_12", typeof(Sprite)) as Sprite;
 }
 
 void FixedUpdate () {
             currentPoolSize = pool.Count;
             if (shouldIDraw) {
                 if (trial == 6) {
                     shouldIDraw = false;
                     FinishWithThat ();
                 }
                 StartSpawningStars ();
 
                 if (changeSprite)
                     starC.GetComponentInChildren<SpriteRenderer>().sprite = realEgg;
             }
             else if (!shouldIDraw) {
                 return;
             }
 }

but it doesn't change the sprite, mainly because the prefabs was instantiate as a clone of the origincal prefabs and it can't find starC to begin with. Any idea?

Comment
Add comment · Show 5
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 aditya007 · May 19, 2017 at 01:09 PM 0
Share

Where are you setting your starC?

avatar image leech54 · May 19, 2017 at 01:34 PM 0
Share

Is starC the prefab or is starC an instantiated GameObject?

Probably need to see you StartSpawningStars() function to see what is going on.

avatar image NoseKills · May 19, 2017 at 01:46 PM 0
Share

Seems suspicious you are setting the sprite from realEgg in FixedUpdate() but you are loading it into a local variable in Awake().

  // This creates a new variable that stores the sprite you load. The variable stops existing as soon as you exit Awake()
 Sprite realEgg = Resources.Load("Egg_12", typeof(Sprite)) as Sprite;
  ...
  // Here you set the sprite from a field you declared at the beginning of your class (?) Thisis not the same variable you loaded in Awake
  starC.
  GetComponentInChildren().sprite = realEgg;

There's nothing wrong with the line with the assignment but perhaps you are not assigning the variable you thought you were.

You need to remove the Sprite from the beginningof this line in order to store the sprite into the field you use in FixedUpdate()

 Sprite realEgg = Resources.Load("Egg_12", typeof(Sprite)) as Sprite;
avatar image sharahamo · May 19, 2017 at 01:58 PM 0
Share

@aditya007 @leech54 @Nose$$anonymous$$ills sorry I missed a line here: supposed during the Awake() before the transform there's starC = (GameObject)Instantiate (Resources.Load ("Prefabs/EggsInvi")); where starC is an instantiated prefab

avatar image sharahamo · May 19, 2017 at 02:01 PM 0
Share

@Nose$$anonymous$$ills apparently making realEgg as a public variable and drag and drop the sprite in inspector works... probably it couldn't find the location of the sprite.

1 Reply

  • Sort: 
avatar image
0

Answer by shadowpuppet · May 19, 2017 at 01:28 PM

I have a sprite change on a prefab in my project but since I know little of coding I take the easy way out. I have BOTH sprites on the prefab and toggle the Sprite renderer On/Off accordingly triggered by a script with a coroutine. My case is an instantiated prefab ( a ragdoll for enemy death) and once instantiated the script runs and 2.3f later the sprite changes

Comment
Add comment · Show 1 · 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 sharahamo · May 19, 2017 at 02:02 PM 0
Share

seem like one of the options too. doesn't kill, right. for now it set the sprite variable to public and drag and drop the sprite into inspector. works but not really neat.

Follow this Question

Answers Answers and Comments

84 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

Related Questions

Overwriting a prefab in runtime? 1 Answer

Saving a spheremodel into a prefab 1 Answer

create prefab at runtime 0 Answers

Lighting Mapping the hard way 1 Answer

Instantiate a prefab vs create one dynamically at runtime? 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