Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

how to write switch statement unity

using UnityEngine;
using System.Collections;

public class ConversationScript : MonoBehaviour 
{
    public int intelligence = 5;
    
    
    void Greet()
    {
        switch (intelligence)
        {
        case 5:
            print ("Why hello there good sir! Let me teach you about Trigonometry!");
            break;
        case 4:
            print ("Hello and good day!");
            break;
        case 3:
            print ("Whadya want?");
            break;
        case 2:
            print ("Grog SMASH!");
            break;
        case 1:
            print ("Ulg, glib, Pblblblblb");
            break;
        default:
            print ("Incorrect intelligence level.");
            break;
        }
    }
}
Comment

scene switch unity


SceneManager.LoadScene("YourScene", LoadSceneMode.Additive);

Comment

item switch unity

using UnityEngine;

public class WeaponSwitching : MonoBehaviour
{
    public int selectedWeapon = 0;

    // Start is called before the first frame update
    void Start()
    {
        selectWeapon();
    }

    // Update is called once per frame
    void Update()
    {
        int previousSelectedWeapon = selectedWeapon;

        if(Input.GetAxis("Mouse ScrollWheel") > 0f)
        {
            if(selectedWeapon >= transform.childCount - 1)
                selectedWeapon = 0;
            else
                selectedWeapon++;
        }
        if(Input.GetAxis("Mouse ScrollWheel") < 0f)
        {
            if(selectedWeapon <= 0)
                selectedWeapon = transform.childCount - 1;
            else
                selectedWeapon--;
        }

        if(Input.GetKeyDown(KeyCode.Alpha1))
        {
            selectedWeapon = 0;
        }

        if(Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 2)
        {
            selectedWeapon = 1;
        }
        if(Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3)
        {
            selectedWeapon = 2;
        }

        if(previousSelectedWeapon != selectedWeapon)
        {
            selectWeapon();
        }
    }

    void selectWeapon()
    {
        int i =  0;
        foreach(Transform weapon in transform)
        {
            if(i == selectedWeapon)
                weapon.gameObject.SetActive(true);
            else
                weapon.gameObject.SetActive(false);
            i++;
        }
    }
}
Comment

scene switch unity

SceneManager.LoadScene("SCENE_HERE");

// be sure to import UnityEngine.SceneManagement
Comment

unity switch

public float number;

switch(number)
{
case 1: 
Debug.Log("1");
break;
case 2:
Debug.Log("1");
break;
default: //case that happens if no case matches
Debug.Log("I dont know");
break;
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: postasjsonasync reference c# 
Csharp :: linq query to check if record exists 
Csharp :: make command prompt hidden c# 
Csharp :: minimize window windows forms application c# 
Csharp :: override gethashcode 
Csharp :: multiplication using arrays 
Csharp :: c# list foreach 
Csharp :: trim c# 
Csharp :: how to check if List<T element contains an item with a Particular Property Value in c# 
Csharp :: c# array of class 
Csharp :: How to find out if a file exists in C# / .NET? 
Csharp :: c# get list item in random order 
Csharp :: wpf get function name 
Csharp :: Unity gameobject visible to specific camera 
Csharp :: c# swap name in string 
Csharp :: how to filter a datatable in c# 
Csharp :: get selected item datagrid wpf 
Csharp :: index of c# 
Csharp :: C# bitwise operation 
Csharp :: c# xml get root attributes 
Csharp :: deserialize json to dynamic object c# 
Csharp :: c# online compiler 
Csharp :: httpget query parameters c# 
Csharp :: Raycasting to find mouseclick on Object in unity 2d games 
Csharp :: c# parse number from string 
Csharp :: c# listview add item 
Csharp :: set file to read only C# 
Csharp :: unity agent look at 
Csharp :: delete all rows from table linq 
Csharp :: working with registry in c# 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =