I’m trying to store some vectors into an array. Here’s what’s going on:
The orange cylinder is the main object “Waypoint”, with the blue “View” objects as its children. In a script applied to the waypoint, I declared an array of Vector3’s, got the children of the object, and got each one’s transform.forward vector. The funny thing is that when I print it, I get four Vector3’s, just like I’d expect. However, when I try to actually put those inside the array, it gives me this error:
BCE0022: Cannot convert ‘UnityEngine.Vector3’ to ‘UnityEngine.Vector3[]’.
What I can gather from this is that I might be accidentally trying to convert the array into a vector3, but I don’t know what the proper syntax would be. Here’s my script so you get the idea:
public var cameraAngles : Vector3[];
function Start ()
{
for (var children : Transform in transform)
{
cameraAngles = children.transform.forward;
print(children.transform.forward);
}
}
Do not use the Array() class. It is slow and untyped and there is nothing it can do that you can’t do with other classes. If you need and array that varies in size, use one of the .NET generic collections. Typically the List class is used. Here is more information on collections in Unity:
#pragma strict
private var cameraAngles : Vector3[];
function Start () {
cameraAngles = new Vector3[transform.childCount];
var i : int = 0;
for (var child : Transform in transform)
{
cameraAngles[i++] = child.forward;
print(child.forward);
}
}
Note that I made ‘cameraAngles’ private since you are initializing it in start. Also with the public array, you have just dragged and dropped the children into the array in the Inspector and not done the initialization in start.