- Home /
How to make a script change what it does depending on what an integer equals
So in this example im trying to make it so if the integer = 1 it says something and updates the integer to 5, then when you click on it again it says something different because it equals something different. Heres my script, any help is greatly appreciated`using System.Collections;
using System.Collections.Generic; using UnityEngine;
public class printscript2 : MonoBehaviour {
int one = 1;
void OnMouseDown()
{
if (one < 2)
{
print("hello");
int one = 5;
Debug.Log(one);
}
if (one > 4)
{
print("welcome to sunnyvale");
}
}
Answer by imrankhanswati · Nov 07, 2017 at 05:01 AM
Hi @Phoenix3666 I am not sure what you want exactly but i will try my best. And i hope this will help.
In one of my project i want to make a reward system in which player will be awarded by watching full ad and the reward type will be the type of player choice so i did it like this.
Step 1:
int playerSelectedAwardType=0; // 0 for no award, 1 for coins and so on...
OnAdFinish()
{
switch(playerSelectedAwardType)
case 0:
print("this is casual ad...");
break;
case 1:
print("Awarding player with coins");
break;
case 2:
print("Awarding player with Potions");
break;
}
step 2: now make another function like this:
AwardPlayerWith(int AwardType)
{
playerSelectedAwardType = AwardType;
}
Step 3: now on button clicke event i just called the function "AwardPlayerWith(value)" with int value.
and the script did what i expected. so i think it might help. in case of any question feel free to ask... :)
thanks, quick question, is that C#?? also is there a way to control when each case happens? Like can I make the Case:1 go after case:0? Thanks for all the help @imrankhanswati
its not c# but close to it. I was just trying give you example. and yes you can do it by just creating a new variable like:
int doneStates;
case 0:
doneState=0; //no need to check doneState because its our first state.
print("this is casual ad...");
break;
case 1:
if(doneState==1)
print("Awarding player with coins");
break;
case 2:
if(doneState==2)
print("Awarding player with Potions");
break;
Answer by TGKG · Nov 08, 2017 at 04:50 PM
An alternate way is to use a Random number generator to change the value of "one".
First: remove the line in your if statement that sets one = 5;
Second: add a line at the bottom of the OnMouseDown method. This new line will change the value of "one" to a different random integer.
int one = 1;
int minInteger = 0;
int maxInteger = 9; //Random.Range will give you an integer between 0 and 8 (9 is excluded)
void OnMouseDown()
{
if (one < 2)
{
print("hello");
Debug.Log(one);
}
if (one > 4)
{
print("welcome to sunnyvale");
}
one = Random.Range(minInteger, maxInteger);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612142518im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Adding Text to Canvas via script 4 Answers
Position Text inside Canvas via Script 2 Answers
Need help limiting Air Movement 0 Answers
error CS0103: The name `Convert' does not exist in the current context? 1 Answer
Calibration of accelerometer with offset 2 Answers