- Home /
Get the random Sprite to trigger two different event
I have been looking around for this 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. This 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");
}
}
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.
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);
}
}
`
Oh I see sorry for misunderstanding the question but I am happy you figured it out !
It's okay, but thanks for helping. I really appreciated it.
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
}
}
Your answer
Follow this Question
Related Questions
Sprite Shadow Script Problem 2 Answers
How to double spirte/gameobject/prefab and control the result on those items? 0 Answers
time.deltaTime timer increasing faster and faster 1 Answer
Plz Need Help 1 Answer
problems with ReplacePrefab script 0 Answers