• 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
Question by symingtonanimation · Dec 06, 2019 at 05:36 PM · scripting problemspritescript.

Get the random Sprite to trigger two different event

I have been looking around for t$$anonymous$$s answer and I can not find it. I want to have two sprites on the same sprite renderer that are been random to call two different events. The first event is to multiply the bet amount that is been called and the second is to stop the game. T$$anonymous$$s what I go so far. Did anyone get any ideas?

    private RandomGameobject randomObjects;
     private SpriteRenderer spriteR;
 
     // Start is called before the first frame update
     private void OnEnable()
     {
         spriteR = gameObject.GetComponent<SpriteRenderer>();
 
         if (GetComponent<SpriteRenderer>().sprite = Bad)
         {
             Debug.Log("BadSprite");
         }
         if (GetComponent<SpriteRenderer>().sprite = reward)
         {
             Debug.Log("RewardSprite");
         }
     }
Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by symingtonanimation · Dec 07, 2019 at 08:07 AM

Thank you @Anis1808 for replaying. Close to what I am looking for, but I want it not to change the sprite, but give it a value or task. I already made it that to random. When the object is active on bad or rewards sprite it will give a reward or stop the game. Is there a way that I could make it when the name of the sprite is called on "OnEnable" like reward sprite it will give a reward. alt text


screenshot-2019-12-07-at-095910.png (437.2 kB)
Comment

People who like this

0 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 symingtonanimation · Dec 07, 2019 at 11:12 AM 0
Share

I work it out that When the name is mention that it will call and action. you can see the code here.`

 private SpriteRenderer spriteR;
 private RandomGameobject randomGameobject_;

 // Start is called before the first frame update
 private void OnEnable()
 {
     string NameSprite = GetComponent<SpriteRenderer>().sprite.name;
     randomGameobject_ = GameObject.Find("ReplaceItems").GetComponent<RandomGameobject>();

     if (NameSprite.Contains("Bad"))
     {
         Debug.Log(NameSprite);
     }
     if (NameSprite.Contains("reward"))
     {
         Debug.Log(NameSprite);
     }
 }

`

avatar image Anis1808 · Dec 07, 2019 at 02:42 PM 0
Share

Oh I see sorry for misunderstanding the question but I am happy you figured it out !

avatar image symingtonanimation Anis1808 · Dec 07, 2019 at 03:59 PM 0
Share

It's okay, but thanks for helping. I really appreciated it.

avatar image

Answer by Anis1808 · Dec 06, 2019 at 06:33 PM

Do you want to trigger the event when the sprites are clicked or when they appear ? If you want to trigger them when they are awakened:

 private SpriteRenderer spriteR;
 public Sprite reward, bad;      //Set the sprites in the Editor
 
 private void OnEnable()
 {
      spriteR = GetComponent<SpriteRenderer>();
      if (spriteR.sprite = bad)
      {
          //Quit the game here
      }
      if (spriteR.sprite = reward)
      {
          //Reward the user here
      }
 }

If you want to trigger them when they are clicked, you need 1 more step: you need to add a BoxCollider to your sprite object.

 private SpriteRenderer spriteR;
 public Sprite reward, bad;      //Set the sprites in the Editor
 
 void OnMouseDown()
 {
      spriteR = GetComponent<SpriteRenderer>();
      if (spriteR.sprite = bad)
      {
          //Quit the game here
      }
      if (spriteR.sprite = reward)
      {
          //Reward the user here
      }
 }
Comment
lgarczyn

People who like this

1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

243 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

Related Questions

How to double spirte/gameobject/prefab and control the result on those items? 0 Answers

Sprite Shadow Script Problem 2 Answers

Manual sprite split 0 Answers

Why when creating new animator controller for the character the character is not walking right ? 0 Answers

How to get pixel coordinates when mouse click on Sprite Renderer 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