- Home /
Question by
dragonking300 · Jul 24, 2017 at 03:21 AM ·
c#visual studionewbie
Null reference exception without a reference to a line?
Normally when you get a error in the console in unity you get a reference to what line the error is caused on but I'm not getting a reference to a line?
using UnityEngine;
using System.Collections;
public class BasicEnemyAiV2 : MonoBehaviour {
private GameObject Player;
private Vector3 newPosition;
public float Timer;
public float TimerStart;
private bool OneTimeExecution = true;
static private Dash PlayerMovementScript;
void Start ()
{
Player = GameObject.FindGameObjectWithTag("Player");
TimerStart = Timer;
PlayerMovementScript = Player.GetComponent<Dash>();
}
public Vector3 PlayerNewPosition = PlayerMovementScript.newPosition;
public bool ThePlayerIsMoving = PlayerMovementScript.IsMoving;
// Update is called once per frame
void Update ()
{
if (Timer <= 0)
{
if (ThePlayerIsMoving && OneTimeExecution == true)
{
Vector3 rayDir = PlayerNewPosition - transform.position;
Ray ray = new Ray(transform.position, rayDir);
Debug.DrawRay(transform.position, rayDir, Color.black, 3);
ThePlayerIsMoving = false;
OneTimeExecution = false;
}
else if (OneTimeExecution == true)
{
Vector3 rayDir = Player.transform.position - transform.position;
Ray ray = new Ray(transform.position, rayDir);
Debug.DrawRay(transform.position, rayDir, Color.black, 3);
OneTimeExecution = false;
}
}
else
{
Timer -= Time.deltaTime;
}
}
}
Comment
Best Answer
Answer by Reynarz · Jul 24, 2017 at 03:34 AM
Are you one hundred percent sure that the player was found with the tag? and change this:
public Vector3 PlayerNewPosition = PlayerMovementScript.newPosition;
public bool ThePlayerIsMoving = PlayerMovementScript.IsMoving;
for this
public Vector3 PlayerNewPosition;
public bool ThePlayerIsMoving;
private void Update()
{
PlayerNewPosition = PlayerMovementScript.newPosition;
ThePlayerIsMoving = PlayerMovementScript.IsMoving;
}
Yes this works but I want a better solution because these variables only need to be called for once. Oh well, it doesn't add noticeable lag doing this so I'll save it for when I'm increasing my script's performance
if you want call it once, don't assign the value to those fields, use the variable Player$$anonymous$$ovementScript directly.