using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InstantiateUnits : MonoBehaviour
{
public Transform Range;
public Transform unitToInstantiate;
public int numberOfUnits;
// Use this for initialization
void Start ()
{
for (int i = 0; i < numberOfUnits; i++)
{
Instantiate(unitToInstantiate, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
Instantiate(Range, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
unitToInstantiate.parent = Range;
}
}
// Update is called once per frame
void Update ()
{
}
}
I want that each created unitToInstantiate to be in the center of created Range.
Range is a GameObject and unitToInstantiate is a Prefab.
But i’m getting exception on the line:
Instantiate(Range, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
InvalidCastException: Cannot cast from source type to destination type.
UnityEngine.Object.Instantiate[Transform] (UnityEngine.Transform original, Vector3 position, Quaternion rotation)
And i’m also not sure that making the unitToInstantiate parent of the Range will create them in the middle of the Range. So i have two problems.