How to convert?

Hey ,
I don’t know what’s wrong with this Code!
after I got some guides,So ,could u please tell me?
The Line 11 has an error

using UnityEngine;
using System.Collections;

public class GameObjectUtil {

    public static GameObject Intantiate(GameObject prefab,Vector3 pos){
        GameObject instance = null;



        instance = GameObject.Instantiate (prefab);
        instance.transform.position = pos;

        return instance;
    }

    public static void Destroy (GameObject gameObject){
        GameObject.Destroy (gameObject);
    }

}

actually I think it’s true:eyes:

  1. What’s the error you get?

  2. There’s already an overload of Object.Instantiate that takes a position:
    Unity - Scripting API: Object.Instantiate

Hi.
The line 11 should be:

instance = GameObject.Instantiate (prefab, pos);

Then no need to line 12.

Try this:

instance = (GameObject)Instantiate(prefab);

It was :
Assets/Scripsts/GameObjectUtil.cs(11,17): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject’. An explicit conversion exists (are you missing a cast?)
[quote=“Stardog, post:5, topic: 688973, username:Stardog”]
Try this:
** **instance = (GameObject)Instantiate(prefab);** **
**[/quote]
**
it doesn’t work.

It’s got to be:

instance = (GameObject)Object.Instantiate(prefab);

Or GameObject.Instantiate(prefab)

Or Object.Instantiate(prefab)

Though, that ladder one should be implicit… not sure why it’s not off the top of my head, probably because the UnityEngine.Object overload exists.

Since you’re in a class that inherits from UnityEngine.Object, you have to declare the containing class the static method is from.

Thank u very much!!!It works.
thanks all!

Why do you keep using bold text?

I Like It.

Please stop, it’s distracting.

2 Likes

lol