- Home /
Javascript Snakegame Error Cannot convert ....Gameobject to .....Transform
I am trying to create a snake game following couple of tutorials, now i am having an error and can not find a way to solve it... maybe someone here could help me.
function addPiece()
{
var newPiece : GameObject = Instantiate(piece, transform.position - (rigidbody.velocity*100), Quaternion.identity);
newPiece.name = "Piece";
(newPiece.GetComponent(SmoothFollow)as SmoothFollow).target = lastPiece;
lastpiece = piece;
}
i get this error Assets/snakeMovement.js(86,71): BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'.
Answer by PAEvenson · Feb 20, 2013 at 07:43 PM
I am guessing the SmoothFollow target is a transform but last piece is a gamobject. Next time add a comment on the code so we can see what line is failing :P.
If thats the case try:
(newPiece.GetComponent(SmoothFollow)as SmoothFollow).target = lastPiece.transform;
yes!!! it worked, sorry about that I'll make sure I will next time :) many thanks!
Your answer
Follow this Question
Related Questions
HELP PLEASE 1 Answer
Trouble making money system 1 Answer
Error BCE0044 plz help 0 Answers
How to use if statement with var? 3 Answers
Animation Crossfade won't play but Animation Play does 1 Answer