- Home /
update() keeps getting called with variable's old (initial) value, why wont the variable keep updated?
in my code I have a variable called 'destination', I initialize it in the 'start()' function and inside the function 'ontriggerenter()', and only inside that function do I update the value, regardless of that, my code seems to keep calling the update function with the original value of the variable (100s of times in a matter of seconds), and once a collision takes place it gets called with the value it should keep after 'ontriggerenter', but then keeps getting called with the original value, its like after the call to 'ontriggerenter' the variable gets changed back to its initial value, why would this happen?
plus I tried debugging the code but without having the stacktrace of the functions that call 'update', i have no way of finding the root of my problem, please help!
// here is my code! ===========================================
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEditor;
using UnityEngine;
public class ball_script : MonoBehaviour
{
Rigidbody rb;
[SerializeField] GameObject well_layer_prefab;
[SerializeField] float speed = 0.005f;
[SerializeField] Vector3 destenation;
GameObject cam;
[SerializeField] float speed_div = 2f;
float lerpTime = 1f;
float currentLerpTime;
// Start is called before the first frame update
void Start()
{
rb = gameObject.GetComponent<Rigidbody>();
cam = GameObject.Find("Main Camera");
var currr = cam.transform.position;
destenation = new Vector3(currr.x, currr.y, currr.z); // tried making a new vector just in case it doesnt get copied when assigned directly from currr
}
// Update is called once per frame
void Update()
{
if (cam.transform.position.y < destenation.y)
{
// problematic part !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// **problematic part** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// problematic part !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
print("tried to change to " + destenation.y+"pos is" + cam.transform.position);
}
else if (cam.transform.position.y > destenation.y)
{
// currentLerpTime += Time.deltaTime;
// if (currentLerpTime > lerpTime)
//{
// currentLerpTime = 0.125f;
// }
// float t = currentLerpTime;
cam.transform.position = destenation;
}
}
private void OnTriggerEnter(Collider other)
{
float new_y_velocity = Mathf.Clamp(GetComponent<Rigidbody>().velocity.y / speed_div, -20f, -0.1f);
GetComponent<Rigidbody>().velocity = new Vector3(0, new_y_velocity, 0);
if (other.gameObject.tag == "goal")
{
// get parent + play animation once + when done destroy
if (other.gameObject.transform.parent)
{
GameObject.Find("soundManager").gameObject.GetComponent<audio_script>().play_audio("pop0");
GameObject.Find("soundManager").gameObject.GetComponent<audio_script>().play_audio("hit");
Animation anim = other.gameObject.transform.parent.GetComponent<Animation>();
anim.wrapMode = WrapMode.Once;
anim.Play("destroy");
Destroy(other.gameObject.transform.parent.gameObject, .5f);
//Vector3 cylinder_size = well_layer_prefab.transform.GetChild(0).gameObject.GetComponent<MeshRenderer>().bounds.size;
// the only update part of destenation !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Vector3 shift_y = new Vector3(0, 0.6f, 0);
destenation -= shift_y;
}
}
}
void OnCollisionEnter(Collision collision)
{
GetComponent<Rigidbody>().velocity = new Vector3(0,3,0);
if (collision.gameObject.tag == "Finish")
{
GameObject.Find("soundManager").gameObject.GetComponent<audio_script>().play_audio("boom");
StartCoroutine(collision.gameObject.GetComponent<TriangleExplosion>().SplitMesh(true));
}
else {
// do nothing
}
}
}
In the end I want to add a link for my whole project if anyone needs it, its very messy though, I apologize. along with the project inside the zip file, there's a video showing what I'm talking about **http://www.mediafire.com/file/pp7hg158tk169tl/circle+bouncer.zip/file**
Answer by xibanya · Oct 11, 2020 at 04:24 AM
If you want to be able to debug when it's set, you could make a property like this
Vector3 Destination
{
get => destenation;
set => destenation = value;
}
And then replace "destenation" with Destination everywhere in the document except when it is first declared. Then you could put a breakpoint on the line with set
and attach the debugger so that it breaks when Destination is set.
xibanya your answer helped me do a little more debugging, eventually i just noticed i had a few objects from the same prefab ( and the prefab contained my script, so the other object was making all those extra calls , thank you so much though, great suggestion.
Your answer
Follow this Question
Related Questions
OnTriggerEnter not working properly. 1 Answer
Problem with OnTriggerExit 1 Answer
Deactivate/Activate FPS Movement 0 Answers
OnTriggerEnter() and OnTriggerExit() called multiple times despite checks. 3 Answers
Multiple Cars not working 1 Answer