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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

error CS0029: Cannot implicitly convert type `GameController' to `UnityEngine.GameObject'

Discussion in 'Scripting' started by Rida2, Jan 24, 2016.

  1. Rida2

    Rida2

    Joined:
    Jan 11, 2016
    Posts:
    35
    How to fix this: Cannot implicitly convert type `GameController' to `UnityEngine.GameObject'
    I have already stated:
    public GameController gameController;
    public GameObject gameControllerObject;
    public GameObject GameController;
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Your variable name begins with a capital, it thinks it's another object. Change it too gameController.
     
  3. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    public GameController gameController;
    public GameObject gameControllerObject;
    public GameObject GameController;
     
  4. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Code (CSharp):
    1. GameController gameControllerTypeComponent = GetComponent<GameController>()
    returns an object of type GameController. Assigning it to a variable of Type GameObject is not going to work and produce the error you got.
    Code (CSharp):
    1. GameObject gameControllerObject = GetComponent<GameController>().gameObject
    This will work since the variable gameObject of any Component will return an object of type GameObject.

    If you are unable to fix your issue with that we need more code to look at.
     
    TaleOf4Gamers likes this.
  5. Rida2

    Rida2

    Joined:
    Jan 11, 2016
    Posts:
    35
    Thanks Guys, I've tried all the above, but still same error (+others)
    Here's the script now:

    //public GameObject gameControllerObject;
    public GameController gameController;
    //public GameObject gameController;



    void Start()
    {GameObject gameController=GameObject.FindWithTag("GameController");

    if(gameControllerObject!= null)
    {
    gameController= gameControllerObject.GetComponent<GameController>();
    }
    if(gameController==null)
    {Debug.Log("Can not find 'Game Controller' script");
    }
    }
     
  6. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Please use code tags to wrap your code and make it readable. Here's an explanation on how to do that:
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    It's quite obvious though:
    Code (CSharp):
    1. public GameController gameController;
    You are defining gameController as a variable of type GameController.
    Code (CSharp):
    1. GameObject gameController=GameObject.FindWithTag("GameController")
    Here you are trying to re-define gameController as type GameObject.

    This is simply not going to work.

    I think what you really want to do is this:

    Code (CSharp):
    1. public GameObject gameControllerObject;
    2. public GameController gameController;
    3.  
    4. void Start()
    5. {
    6.     gameControllerObject = GameObject.FindWithTag("GameController");
    7.  
    8.     if(gameControllerObject != null)
    9.     {
    10.         gameController = gameControllerObject.GetComponent<GameController>();
    11.     }
    12.  
    13.     if(gameController == null)
    14.     {
    15.         Debug.Log("Can not find 'Game Controller' script");
    16.     }
    17. }
    • Declare variable types once, either at the beginning of the class or as a local variable within your methods.
    • When assigning values to variables, you do not need to declare the type again if that has been done before.
    • Use a naming which helps you to easily identify which variable serves which purpose. Having GameController, GameControllerObject, GameControllerComponent and so all declared as variables at the same time will almost certainly cause confusion for you and anyone else trying to read and understand your code.
    From your questions I strongly recommend to have a look at C# basics about fields, properties, methods, value assignment and alike first.
     
  7. Rida2

    Rida2

    Joined:
    Jan 11, 2016
    Posts:
    35
    Thanks Manlaiken,
    Thanks Glockenbeat'
    ...
    Works fine except needed L6.: GameObject gameControllerObject....
    Rida2