#Need Help With Raycast Based Timer

1 messages · Page 1 of 1 (latest)

terse birch
#

Basically I need help with getting my thing to be turned False if the raycast is not on it

This is the interactor
[
if (Input.GetKey(KeyCode.E))
{
InteractWithObjectRHold();
holdR = true;
}
else
{
holdR = false;
}

void InteractWithObjectRHold()
{
RaycastHit hit;
Ray ray = new Ray(cam.transform.position, cam.transform.forward);
if (Physics.Raycast(ray, out hit, 2f, Clothes))
{
Clothes Cloth = hit.collider.GetComponent<Clothes>();
Debug.Log("Searching");
Cloth.test(holdR);
}
}] cb

Here is the other script

[ void Update()
{
if (Timer > 0 && count)
{
Debug.Log("Found");
Timer -= Time.deltaTime;
}
else if (Timer <= 0 && count)
{
print("done");
ClothManage cManager = GetComponentInParent<ClothManage>();
cManager.clothesChecked++;
Destroy(gameObject);
}
else
{
// Reset state or perform other actions when not hit by raycast
ResetState();
}
}

public void test(bool bada)
{

   count = bada;

   if (count)
   {
       Debug.Log("do");
   }
   else
   {
       Debug.Log("don't");
   }

}

public void ResetState()
{
count = false;
}]cb

I know it has something to do with the interaction script but I can't figure it out. Can someone quickly help please?

twilit raft
#

bools are value types, they are not passed by reference. Whatever you set holdR to in your interaction script, it will not influence the value stored in your Cloth script.
Since you are only ever calling Cloth.test when holdR is true, that is the only value your Cloth script will get.
If you want to reset it, you need to keep track of the last Cloth object you interacted with, and then call test(false) when the interaction ends (i.e. the interaction key is let go, or the raycast does not hit the object anymore).