2
votes

I want to place six objects(ball) on scene. I think the code look workable but I receive a console message. The message :

"Assets/GameScripts/Instance.cs(26,40): error CS0266: Cannot implicitly convert type object' toUnityEngine.Vector3'. An explicit conversion exists (are you missing a cast?)"

using UnityEngine; using System.Collections;

public class Instance : MonoBehaviour { public GameObject ball;

public ArrayList coordinateContainer = new ArrayList();



// Use this for initialization
void Start () {

    coordinateContainer.Add(new Vector3(1f,1f,1f));
    coordinateContainer.Add(new Vector3(2f,1f,1f));
    coordinateContainer.Add(new Vector3(3f,1f,1f));
    coordinateContainer.Add(new Vector3(4f,1f,1f));
    coordinateContainer.Add(new Vector3(5f,1f,1f));
    coordinateContainer.Add(new Vector3(6f,1f,1f));


    //ball.transform.position = new Vector3(1f,1f,1f);
    ball.transform.rotation = Quaternion.identity;

    for (int i = 0; i <  6; i++) {
        ball.transform.position = coordinateContainer[i];
        Instantiate(ball,ball.transform.position,ball.transform.rotation);
    }
}

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

}

}

1

1 Answers

5
votes

Since you're using an ArrayList the vectors are being stored as objects. Try this

ball.transform.position = (Vector3)coordinateContainer[i];

You may be better off with List<Vector3> instead of an ArrayList so that you can avoid the casting (as List<T> can only hold objects of type T).