Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why is it showing error?

Discussion in 'Scripting' started by Amaan-Sadri-Wala, Mar 12, 2016.

  1. Amaan-Sadri-Wala

    Amaan-Sadri-Wala

    Joined:
    Jun 9, 2015
    Posts:
    14
    This is the script, I tried input.getkeydown with switch statements but it is showing error, can anyone tell me the reason behind this? Help Plz!

    using UnityEngine;
    using System.Collections;

    public class SwitchStatements : MonoBehaviour
    {

    // Use this for initialization
    void Start ()
    {
    switch (gameObject)
    {
    case (Input.GetKeyDown(KeyCode.R)):
    GameObject.GetComponent <Renderer> ().material.color = Color.red;
    break;
    case (Input.GetKeyDown(KeyCode.G)):
    GameObject.GetComponent<Renderer> ().material.color = Color.green;
    break;
    case (Input.GetKeyDown(KeyCode.B)):
    GameObject.GetComponent <Renderer> ().material.color = Color.blue;
    break;
    default:
    Debug.Log ("Invalid");
    }
    }

    // Update is called once per frame
    void Update ()
    {


    }
    }
     
    Last edited: Mar 12, 2016
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,903
  3. Amaan-Sadri-Wala

    Amaan-Sadri-Wala

    Joined:
    Jun 9, 2015
    Posts:
    14
    hello mgear yeah I know tat I have placed it inside start() function but in the update() function too the same thing is happening! the error line is :Assets/Fundamentals/SwitchStatements.cs(10,17): error CS0151: A switch expression of type `UnityEngine.GameObject' cannot be converted to an integral type, bool, char, string, enum or nullable type
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Have you followed the link? You cannot just put in there whatever you want to. Basically, there are restrictions for types in the switch part and the cases need to be constant values.
     
  5. Amaan-Sadri-Wala

    Amaan-Sadri-Wala

    Joined:
    Jun 9, 2015
    Posts:
    14
    Hello Suddoha, yeah i now know about it thnx for help!
     
  6. Amaan-Sadri-Wala

    Amaan-Sadri-Wala

    Joined:
    Jun 9, 2015
    Posts:
    14