- Home /
Declaring a Vector3 variable in C#
Can a Vector3 be a variable? I thought so, but I can't seem to find the correct syntax. How do I make this work?
for (float i = 0 ; i < blockX; i++)
{
Vector3 rowX = (i,0,0); //error on this line
Rigidbody instance = Instantiate (block,rowX, 0 );
}
This gives me the error: error CS1026: Unexpected symbol `,', expecting `)'
I have also tried skipping the declaration and just putting:
Rigidbody instance = Instantiate (block,Vector3(i,0,0),0);
That gives me more errors though.
Answer by Jessespike · Sep 10, 2012 at 12:41 AM
Vector3 isn't a variable, it's a struct. You need create it instead of just assigning it, you were missing "new Vector3"
for (float i = 0 ; i < blockX; i++)
{
Vector3 rowX = new Vector3 (i,0,0); //error on this line
Rigidbody instance = Instantiate (block,rowX, 0 );
}
Vector 3 is a Struct, eh? If this is the case, and you're supposed to call the constructor method of that Struct with the 'new' keyword on the right side of the equal sign, what is the purpose of writing this:
Vector3 relativePosition = target position -transform.position;
transform.rotation = Quaternion.LookRotation(relativePosition);
I know what and why that code works, but that "Vector 3 Struct" sure as hell looks kind of like a variable declaration to me!!!
There is no use of the 'new' keyword, and Vector 3 appears only on the left side of the equal sign. So you're saying,then, that 'relativePosition' is not a variable, but a Struct? Are you or are you are not storing a reference to that Vector 3 in a variable? Please elaborate...