- Home /
Float randomly being set to 0 spontaneously
In the script we have a float, distanceLimit. It is defined in the inspector and is never changed in any script. However when debugging, it will spontaneously change to 0 for seemingly no reason. I have never seen anything like this and couldn't find any other similar problems. Any help would be appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ememyAI : MonoBehaviour
{
public float distanceLimit;
public int time;
public NavMeshAgent enemy;
[HideInInspector]
public Transform PlayerTransform;
public GameObject bullet;
public GameObject BobAni;
// Start is called before the first frame update
void Start()
{
PlayerTransform = GameObject.Find("XR Rig").GetComponent<Transform>();
enemy.destination = PlayerTransform.position;
}
public void Shoot()
{
enemy.isStopped = true;
if (time > 60)
{
GameObject Bul = Instantiate(bullet, transform.position, Quaternion.identity);
time = 0;
BobAni.SendMessage("ShootCheck");
}
}
// Update is called once per frame
void Update()
{
time++;
float Distance = Vector3.Distance(PlayerTransform.position, enemy.gameObject.transform.position);
if (Distance < distanceLimit)
{
RaycastHit hit;
// Does the ray intersect any objects excluding the player layer
if (Physics.Raycast(transform.position, (PlayerTransform.position - transform.position).normalized, out hit, Mathf.Infinity))
{
if (hit.collider.gameObject.name == "XR Rig")
{
Debug.DrawRay(transform.position, (PlayerTransform.position - transform.position).normalized * hit.distance, Color.green);
Debug.Log("Did Hit");
Shoot();
}
else
{
Debug.DrawRay(transform.position, (PlayerTransform.position - transform.position).normalized * 1000, Color.red);
Debug.Log("Did not Hit");
}
}
else
{
Debug.DrawRay(transform.position, (PlayerTransform.position - transform.position).normalized * 1000, Color.yellow);
Debug.Log("Did not Hit");
}
}
else
{
enemy.isStopped = false;
}
}
}
After running the program for some time, distanceLimit randomly is set to 0. Yet this is only true for when it is called inside of this function. When distanceLimit is referenced in another script at the same time, the distance limit is defined as the value we set.
Answer by Bunny83 · Dec 17, 2021 at 11:07 PM
There are a few things not clear here. First of all you can only "call" methods. Fields can only be accessed / read or set / written to. You said it only happens when you access the field inside "this function". Which method do you mean? Inside Update?
You also said "when debugging". What exactly do you mean by that? Where and how do you actually observe the value? In the inspector? In Visual Studio when stepping through code?
Pretty much 90% of all cases where people say that some fields magically change their value are a result of you're looking at the wrong object. Maybe you somehow execute the method in question on a prefab object in the project rather than an object in the scene? Have you tried adding Debug.Log statements to your code and logging the actual value? Note that you can add a context reference to Debug.Log as second parameter. This makes it easy to figure out which object actually produced the log message.
Debug.Log("distanceLimit = " + distanceLimit, gameObject);
Your answer
Follow this Question
Related Questions
Why do I need the "f" when using a float? 1 Answer
Scaled Variables 2 Answers
HOW TO ADD AND REMOVE VARIABLES ALREADY DECLARED IN A LIST 1 Answer
How do I add something on to a float. 1 Answer
random respawn and respawn delay 1 Answer