- Home /
Missing definitions and script error
This script makes an animated AI wander around a game object, however the console is displaying these errors and i have no idea how to fix them.
Here's the script using UnityEngine; using System.Collections;
public class RandomWander : MonoBehaviour {
public GameObject init;
private Transform initpos;
private float time = 10;
private bool update = true;
public float radius;
Animator anim;
float lowX;
float highX;
float lowZ;
float highZ;
// Use this for initialization
void Awake () {
anim = gameObject.GetComponent <Animator>();
initpos = init.GetComponent<Transform>();
lowX = initpos.x - radius;
highX = initpos.x + radius;
lowZ = initpos.z - radius;
highZ = initpos.z + radius;
}
// Update is called once per frame
void Update () {
if (update) {
Vector3 pos = getRandomPos();
}
gameObject.LookAt(pos);
if (gameObject.transform.position == pos){
time -= Time.deltaTime;
anim.SetBool("Walking", false);
if (time <= 0.0f){
update = true;
time = 10.0f;
}
} else {
transform.position += transform.forward * 0.2f;
anim.SetBool("Walking", true);
}
}
Vector3 getRandomPos(){
Vector3 pos;
pos.x = Random.Range(lowX, highX);
pos.y = initpos.y;
pos.z = Random.Range(lowZ, highZ);
return pos;
}
}
If anyone can resolve these issues it would be greatly appreciated.
Answer by AurimasBlazulionis · Oct 30, 2016 at 07:10 AM
When you are trying to read position values, you are trying to read them from the Transform
type. It is wrong. Positions are in a Vector3
type named position
inside Transform
type. So, you need to use initpos.position.x
instead of initpos.x
.
It fixed most issues but it now says - error CS1061: Type UnityEngine.GameObject' does not contain a definition for
position' and no extension method position' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
Also any idea on how to fix - error CS1061: Type UnityEngine.GameObject' does not contain a definition for
LookAt' and no extension method LookAt' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)