can anyone help me really fast, i made a code making my enemy target the player and draw a line between them but the line dosent show and i got a null error, here is the null error
NullReferenceException
UnityEngine.Transform.get_position () (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:19)
EnemyAI.Update () (at Assets/scripts/EnemyAI.cs:24)
and here is the code
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotaionSpeed;
private Transform myTransform;
void awake(){
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag ("Player");
target = go.transform;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(target.position ,myTransform.position , Color.yellow );
}
Change the:
void awake() {
...
to
void Awake() {
...
Also you don’t need to assigned yourself. if you are referencing the transform of the object this monobehavior script is attached to, then just the property name(s) or this.propertyName will work fine… like:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotaionSpeed;
// private Transform myTransform; // not needed
// empty Awake is a waste of processing
// void Awake(){
// }
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag ("Player");
target = go.transform;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(target.position, this.transform.position, Color.yellow); // remove the this and it will work too as that property is derived from monobehavior
}
Actually - this thread proved that caching the transform nets you a performance gain.
alright that worked acctully but now my enemy dosent move towards me anymore must been writing something wrong again