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

Convert 'UnityEngine.Object' to `UnityEngine.GameObject'.

Discussion in 'Android' started by Dosorvet, Jan 16, 2018.

  1. Dosorvet

    Dosorvet

    Joined:
    Jan 16, 2018
    Posts:
    17
    Hey guys,
    I'm trying to create a very simple android game with Unity Engine..
    But I have a problem,
    when I wanna Run the game , the Console says:
    Assets/Scripsts/GameObjectUtil.cs(9,17): error CS0266: Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)
    What shall I do?
    I thing there is no problem in my C# scrpits,because it was tested before,
    So,maybe I forgot to add some library..or something like that,I don't know,
    Please give me guidance.
    Thanks.
     
    Last edited: Jan 16, 2018
  2. Dosorvet

    Dosorvet

    Joined:
    Jan 16, 2018
    Posts:
    17
    here it is:
     
  3. Dosorvet

    Dosorvet

    Joined:
    Jan 16, 2018
    Posts:
    17
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameObjectUtil {
    5.  
    6.     public static GameObject Intantiate(GameObject prefab,Vector3 pos){
    7.         GameObject instance = null;
    8.  
    9.         instance = GameObject.Instantiate (prefab);
    10.         instance.transform.position = pos;
    11.  
    12.         return instance;
    13.     }
    14.  
    15.     public static void Destroy (GameObject gameObject){
    16.         GameObject.Destroy (gameObject);
    17.     }
    18.  
    19. }
     
  4. Dosorvet

    Dosorvet

    Joined:
    Jan 16, 2018
    Posts:
    17
    line number 9 has an error
    Line number 9 has an error
     
  5. Dosorvet

    Dosorvet

    Joined:
    Jan 16, 2018
    Posts:
    17
  6. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,844
    I don't understand, why did you post this in Android forum part, but whatever.
    https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
    It returns and object of type "Object", which you're trying to assign to a variable of type "GameObject", which is impossible without an explicit cast, since GameObject is derived from Object (e.g. all objects of type GameObject are also objects of type Object, but the reverse is not true). Since you know you're calling "instantiate" on a game object, you can just use a cast.
     
    KarlKarl2000 and hopetolive like this.
  7. JuliusM

    JuliusM

    Unity Technologies

    Joined:
    Apr 17, 2013
    Posts:
    824
    In the link that you were given there are code examples where casting is performed. Just look at the examples and you'll see how they differ from your script. Your original error message is very informative and explains exactly what is wrong. Usually the first thing you should do is to google the error or the concepts mentioned in the error if you don't know or don't understand them. Reading this https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions should make things clearer for you.
     
    KarlKarl2000 likes this.
  8. Dosorvet

    Dosorvet

    Joined:
    Jan 16, 2018
    Posts:
    17
    aha,thanks
    but u know?
    I'm not exactly a game developer!
    I don't even know the C# language well,but i'm trying to learn it,I need to create my Game as soon as possible,so,,,I need some guide.
     
  9. Rootbeard

    Rootbeard

    Joined:
    Nov 10, 2015
    Posts:
    6
    Not your fault, Dosorvet. This kind of faux helpfulness is simply gross elitism. These big heads know they could post a code sample, but they live under the delusion that they should talk over people to "help them learn". Indeed, how dare you set foot in the Unity forums without a CS degree?
     
  10. troyden101

    troyden101

    Joined:
    Sep 6, 2015
    Posts:
    1
    Exactly, forum's purpose is to help people. Those who do not need help should either be happy or should be happy helping others.
     
  11. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    592
    I didn't understand the term Cast until you shared this link. Now I know what I've been doing this time was -> casting. :)