Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CaptchaIsStupid · Dec 17, 2021 at 10:22 PM · variablesfloatlogicproblem during runtimevariable-definition

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

random respawn and respawn delay 1 Answer

Xf notation vs just a number? 1 Answer

GameObject.Find either can't be called or is in the wrong scope. 0 Answers

Variables to represent the edges of the screen 1 Answer

Primitive data types (for code, not modeling) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges