sum of vector error :(

Hi guys,

in the function Spin, I am basically making the sum of 2 Vector3, but for whatever reason the compiler returns me that ‘+ can’t have left hand a vector and right and an object’ error, which I don’t think it’s true but can’t see it… the error should be in the FOR at the bottom of the function. Many thanks for the help.

for (dir in previousDirList) {
		avgPreviousDir = avgPreviousDir + dir;
	}
function Spin (dir : Vector3) {
	var hit : RaycastHit;
	var previousDirList : Array = new Array ();
	var currentDir : Vector3;
	
	// Initialize previous dir list
	for (var i : int = 0; i < numberAverages; i++) {
		previousDirList.Add (currentDir);
	}
	
	currentDir = dir;
	
	// Make the object rotate with the cursor while we are grabbing it
	while (Input.GetButton ("Fire1")  Physics.Raycast (camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
		// Remove first element of the array
		previousDirList.RemoveAt (0);
		// Add current dir to the end
		previousDirList.Add (currentDir);
		currentDir = hit.point - transform.position;
		transform.rotation =  Quaternion.LookRotation (currentDir) * offsetRotation * originalRotation;
		yield;
	}
	
	// User let go of the mouse so make the object spin on its own
	var avgPreviousDir : Vector3 = Vector3.zero;
	
	for (dir in previousDirList) {
		avgPreviousDir = avgPreviousDir + dir;
	}
	
	avgPreviousDir /= numberAverages;
	Kick (currentDir, avgPreviousDir);
}

Hi giancamati,

previousDirList is an array not an Vector3 array so you must cast ‘dir’ to may use + operator.

actualy you try to do Vector3 = Vector3 + Object

( you also use += operator in your case )

Julien G.

That means what it says…the left hand is a Vector3 and the right hand is Object. You have to cast your variables correctly. It will help if you don’t use the JS Array class, which is very slow and not type safe.

–Eric

Ohh Ok!.. so what kind of Array should I use?

Thank you all for the useful suggestions!
GC.

Use a built-in array if you know the size beforehand, or a List otherwise.

–Eric

built in.

var myVectorArray : Vector3[] = new Vector3[10];

or use lists, queues and stacks:

import System.Collections.Generic;

var list: List.<int> = new List.<int>();
var stack : Stack.<int> = new Stack.<int>();
var queue : Queue.<int> = new Queue.<int>();

Love you guys (methaphorically) thank you for the help !:slight_smile: