"Cannot convert 'UnityEngine.Transform' to 'UnityEngine.Vector3'." Error Message

I am a bit of a noob when it comes to coding, but I made two scripts, one is unfinished (I haven’t implemented the touch function yet) and one is finished and working. They both declare this at the start:

var lastPosition : Vector3;
var myTransform : Transform;

The first script works fine, no error messages whatsoever but the second one brings up a “Cannot convert ‘UnityEngine.Transform’ to ‘UnityEngine.Vector3’.” Error Messages at line 38 and line 50.

First Script:

var lastPosition : Vector3;
var myTransform : Transform;
var isMoving : boolean;
var LeftArm : GameObject;
var RightArm : GameObject;
var GameCamera : GameObject;
var MainCharacter : GameObject;

function Start() {
	myTransform = MainCharacter.transform;
	lastPosition = myTransform.position;
	LeftArm.animation["LeftArm"].wrapMode = WrapMode.Once;
	RightArm.animation["RightArm"].wrapMode = WrapMode.Once;
	GameCamera.animation["HeadBobLeft"].wrapMode = WrapMode.Once;
	isMoving = false;
} //This is declaring the variables at the start.

function Update()
{
 for (var touch : Touch in Input.touches)
  {
   if (guiTexture.HitTest (touch.position))
	{
//Does everything inside these comments if the GUI button is being touched.
     if(myTransform.position != lastPosition){
		isMoving = true;
		LeftArm.animation.Play("LeftArm");
		RightArm.animation.Play("RightArm");
		GameCamera.animation.Play("HeadBobLeft");
		lastPosition = myTransform.position;
	}
	
	else {
		isMoving = false;
		lastPosition = myTransform.position;
		}
//Does everything inside these comments if the GUI button is being touched.
	}
  }
}

The Second one, which is a little bit longer:

var leftArmPlayed : boolean = false;
var rightArmPlayed : boolean = false;
var leftBobPlayed : boolean = false;
var rightBobPlayed : boolean = false;
//Advanced variables.
var lastPosition : Vector3;
var myTransform : Transform;
var isMoving : boolean;
var LeftArm : GameObject;
var RightArm : GameObject;
var GameCamera : GameObject;
var MainCharacter : GameObject;
//General variables.

function Start() {
	myTransform = MainCharacter.transform;
	lastPosition = myTransform.position;
	LeftArm.animation["LeftUarm"].wrapMode = WrapMode.Once;
	LeftArm.animation["LeftDarm"].wrapMode = WrapMode.Once;
	RightArm.animation["RightUarm"].wrapMode = WrapMode.Once;
	RightArm.animation["RightDarm"].wrapMode = WrapMode.Once;
	GameCamera.animation["HeadBobLeft"].wrapMode = WrapMode.Once;
	GameCamera.animation["HeadBobRight"].wrapMode = WrapMode.Once;
	isMoving = false;
}
//Declaring all the variables at the start.

function Update(){

	if(myTransform != lastPosition && leftArmPlayed == true && LeftArm.animation.isPlaying == false && leftBobPlayed == true){
		LeftArm.animation.Play("LeftDarm");
		RightArm.animation.Play("RightUarm");
		GameCamera.animation.Play("HeadBobRight");
		leftArmPlayed = false;
		rightArmPlayed = true;
		leftBobPlayed = false;
		rightBobPlayed = true;
		lastPosition = myTransform;
		isMoving = true;
	}
	
	if(myTransform != lastPosition && rightArmPlayed == true && RightArm.animation.isPlaying == false && rightBobPlayed == true){
		LeftArm.animation.Play("LeftUarm");
		RightArm.animation.Play("RightDarm");
		GameCamera.animation.Play("HeadBobLeft");
		rightArmPlayed = false;
		leftArmPlayed = true;
		leftBobPlayed = false;
		rightBobPlayed = true;
		lastPosition = myTransform;
		isMoving = true;
	}
}

Did you manage to solve this issue? please post the solution that worked with you.

–

1 Answer

1

Line 38 should be:

lastPosition = myTransform.position;

Line 42 should have

myTransform.position = lastPosition;

Line 50 should be:

lastPosition = myTransform.position;

In each case, you need to add ‘.position’ to ‘myTransform’.

Thanks for your suggestion, but it didn't fix it. It turns out that it was something to do with stating the change in variable twice, so I declared it as a function and replaced lines 38 and 50 with a statement calling the function and now it works perfectly. Finished it now and it's 90 lines ;D Up voting for your time and effort, thanks!

–