- Home /
Unknown identifier
Hi there, I'm trying to make an object (the object is named "dodecahedron") animate by a player coming into a close proximity of said object - I was given this script earlier on.
var target : Transform;
var detectRange: float = 30;
function Update() {
var tgtDirection = target.position - transform.position;
var tgtDistance = tgtDirection.magnitude;
if (tgtDistance <= detectRange) {
dodecahedron.animation.Play("Animation");
}
}
HOWEVER when I try and input the code I get this message: "Assets/Proximity.js(8,1): BCE0005: Unknown identifier: 'dodecahedron'."
What am i doing wrong?
The error appears because you haven't set "dodecahedron" as a variable in the script.. You could try this :)
var dodecahedron : GameObject;
var target : Transform;
var detectRange: float = 30;
function Start() {
dodecahedron = GameObject.Find("dodecahedron");
}
function Update() {
var tgtDirection = target.position - transform.position;
var tgtDistance = tgtDirection.magnitude;
if (tgtDistance <= detectRange)
{
dodecahedron.animation.Play("Animation");
}
}
I've attached that script to the shape - but it just seems to play the animation on a loop. In the inspector I can see something called 'detect range' I've noticed that when i put the value below 0 it doesnt animate - am i anywhere near where i want to be?
Answer by venkspower · Apr 26, 2012 at 10:25 AM
That is because you have never created a "dodecahedron" GameObject in your script.
Answer by fafase · Apr 26, 2012 at 11:05 AM
I think you need to change your approach. You have this dodecahedron object right?
So attach to him a script like this
var player:GameObject;
var distance:int = 30;
var animNotPlayed:boolean;
function Start(){
player = GameObject.Find("Player"); // Find the player
animNotPlayed=true;
}
function Update(){
if(animNotPlayed){
// Here I changed player.position to player.transform.position.
if(Vector3.Distance(player.transform.position,transform.position)<distance){ // Check distance
animation.Play("Animation"); //Play animation
animNotPlayed=false;
}}}
as a side note, the animNotPlayed is true as long as you hav not played(obviously) once you reach the distance, the anim is played and the bool turns to false, so that the distance is not checked anymore and the animation is not played again.
Im getting this error message when attaching the code you provided "Assets/dodec.js(5,28): BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'."
I'm a complete unity newbie so i only have a very, very basic grasp of what to do
I was expecting that actually. this is because the player is a gameobejct and player is of type Transform. I edit quickly.
Your answer