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 /
avatar image
0
Question by Phoenix3666 · Nov 07, 2017 at 04:33 AM · scriptingbasicsbasic programming

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");
     }
 }
Comment
Add comment
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
0

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... :)

Comment
Add comment · Show 2 · 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 Phoenix3666 · Nov 07, 2017 at 07:44 PM 0
Share

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

avatar image imrankhanswati Phoenix3666 · Nov 08, 2017 at 11:38 AM 0
Share

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;
avatar image
0

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);
     }

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

77 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

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


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