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 /
  • Help Room /
avatar image
0
Question by Gunging · Sep 20, 2016 at 05:17 PM · 2dsprite

how to hange Sprites depening on a float value?

I have a gameobject that every 5 seconds spawns an Enemy prefab, and that enemy can have 5 different weapons, randomly chosen, and with each weapon, it has a different sprite, I got the weapons working, but the sprite wont change, can somebody help me?

     private float Type;  //Type is the random value
     public Sprite Pistol;  //Sprite with a Pistol
     public Sprite Shotgun;  //Sprite with a Shotgun
     public Sprite SniperRifle;  //Sprite with a Sniper Rifle
     public Sprite Flamethrower;  //Sprite with a Flamethrower
     public Sprite Bazooka;  //Sprite with a Bazooka
     //*Al Sprites have been assigned in the editor
 
     void Awake() //Its called before Start
     {
         Type = Random.Range(0, 10); //Type is assigned randomly between 0 and 9
         Debug.Log("" + Type); //Just to see what was chosen
     }
     
     void Start()
     {
         if (Type < 3 && Type >= 0) //If value is 0 or greater and less than 3
         { // enemies with Type 0, 1 and 2 will have pistol
             gameObject.GetComponent<SpriteRenderer>().sprite = Pistol;
         }
         if (Type < 5 && Type >= 3) //If value is 3 or greater and less than 5
         { //enemies with Type 3 and 4 will have Shotgun
             gameObject.GetComponent<SpriteRenderer>().sprite = Shotgun;
         }
         if (Type == 5) //If Type is 5
         {  //Only enemies with Type 5 will have a bazooka
             gameObject.GetComponent<SpriteRenderer>().sprite = Bazooka;
         }
             if (Type < 8 && Type >= 6) //If Type is 6 or greater and less than 8
         {  //Enemies with Type 6 and 7 will have a Sniper Rifle
             gameObject.GetComponent<SpriteRenderer>().sprite = SniperRifle;
         }
         if (Type < 10 && Type >= 8) //If Type is 8 or greater and less than 10
         {  //Enemies with Type 8 and 9 will have Flamethrower
             gameObject.GetComponent<SpriteRenderer>().sprite = Flamethrower;
         }
       //As you can see, Pistol is the most common and Bazooka the rarest
     }
     

I dont know why it wont work, I mean, It wont ever change from Pistol Sprite, which is already assigned to the prefab in the editor, and I dont want guys with a Pistol shooting rockets.

Thanx

Comment
Add comment · Show 1
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 iabulko · Sep 20, 2016 at 05:34 PM 0
Share

In C# all variables should be written starting with small letter, not capital letter (i.e. pistol, not Pistol) . That usually makes problems. Capital letter is reserver for class/function names etc. Try changing that

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Gunging · Sep 20, 2016 at 06:39 PM

I tried changing the capital letters to normal ones, still doesnt work :c

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2d rotate sprite when jumping and falling in C# 1 Answer

How to Update Objects Sprite to the Network 1 Answer

particle system and sprite flipping 0 Answers

How to fix animation "auto-smoothing"? 0 Answers

How to use sprite swapping with Unity's bone rigging 2DAnimation? 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