Home > Blockchain >  Problem with Argument 2: cannot convert from 'UnityEngine.Vector3' to 'UnityEngine.Tr
Problem with Argument 2: cannot convert from 'UnityEngine.Vector3' to 'UnityEngine.Tr

Time:08-18

I been working on this project on unity with youtube tutorial but i got some problems in code. This is my first project ever and i don't know any thing about coding. Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformManager : MonoBehaviour
{
    [SerializeField]
    private GameObject[] _platformPrefabs;

    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; 1 < _platformPrefabs.Length; i  )
        {
            global::System.Object value = Instantiate(_platformPrefabs[i], new Vector3(0, 0, i * 12));
        }
    }

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

CodePudding user response:

You are calling Instantiate with two parameters which is only overloaded for setting the parent, a Transform.

What you want to do is call it with three parameters and also pass a Quaternion for rotation. If you just want the default rotation, pass Quaternion.identity.

Instantiate(_platformPrefabs[i], new Vector3(0, 0, i * 12), Quaternion.identity);

CodePudding user response:

Instantiate is a function with many overloads, so it might not become apparent right away that the compiler expects something different than what you give.

In your case:

Instantiate(_platformPrefabs[i], new Vector3(0, 0, i * 12));

you use two arguments which means that the override

public static Object Instantiate(Object original, Transform parent); 

is used.

You probably want to use:

public static Object Instantiate(Object original, Vector3 position, Quaternion rotation); 

which you can do in your code, by simply adding the Quaternion.identity, so that the offending line reads:

global::System.Object value = Instantiate(_platformPrefabs[i], new Vector3(0, 0, i * 12), Quaternion.identity);
  • Related