- Home /
Question by
NoahDaNerd · Oct 11, 2020 at 05:38 PM ·
vroculus riftstackoverflow
Stack Overflow Error and I cant find any recursive methods
I am trying to create a system for object interaction in VR, but whenever I release the grip, the release function causes a stack overflow.
This is called on the hand whenever the value of the grip is changed (does not seem to be the issue):
private void Grip(float val)
{
anim.SetFloat("Grip", val);
if (val > gripButtonThreshold && !isGrabbing)//If holding down grip (only at start)
{
//find closest object
GameObject closestInteractable = FindObjectOfType<Interactable>().gameObject;
foreach (Interactable obj in FindObjectsOfType<Interactable>())
{
if (Vector3.Distance(holdPoint.position, obj.transform.position) < Vector3.Distance(holdPoint.position, closestInteractable.transform.position))
{
closestInteractable = obj.gameObject;
}
}
closestObject = closestInteractable;
isGrabbing = true;
}
if (val < gripButtonThreshold)//If not holding down grip
{
if (isGrabbing)
{
isGrabbing = false;
}
if (hasObject)
{
holding.GetComponent<Interactable>().Release(this);//Call release on interactable script
}
}
}
This is the code on the interactable script (this seems to be the issue):
private void OnTriggerStay(Collider other)
{
if (other.TryGetComponent(out Hand hand) && hand.isGrabbing && hand.closestObject == gameObject && !hand.hasObject)//if a hand is grabbing and colliding with you (only called once)
{
//Call method set in inspector (Pick Up in Movable Object)
for (int i = 0; i < OnInteract.GetPersistentEventCount(); i++)
{
((MonoBehaviour)OnInteract.GetPersistentTarget(i)).SendMessage(OnInteract.GetPersistentMethodName(i), hand);
}
}
}
public void Release(Hand hand)//called by hand once, but causes stack overflow
{
//call method set in inspector (release in MovableObject)
for (int i = 0; i < OnRelease.GetPersistentEventCount(); i++)
{
((MonoBehaviour)OnRelease.GetPersistentTarget(i)).SendMessage(OnRelease.GetPersistentMethodName(i), hand);
}
}
Finally, Here is the code on movable object (this might be the issue):
public void Release(Hand script)//called from interactable through inspector
{
rb.isKinematic = false;//make the rigidbody do stuff now
rb.velocity = velocity;//set the velocity to the velocity while kinematic
beingHeld = false;
transform.parent = null;//unparent from hand
script.holding = null;//set handto be not holding
script.hasObject = false;//set hand to be not holding
}
public void PickUp(Hand hand)//called from interactable through inspector
{
hand.hasObject = true;//set hand to be holding
rb.isKinematic = true;//set rigidbody to be still
hand.holding = gameObject;//set hand to hold self
beingHeld = true;
holdPoint = hand.holdPoint;
}
Comment
Best Answer
Answer by NoahDaNerd · Oct 14, 2020 at 09:18 PM
It was because I had the methods in the movable object and interactable set the same. I do not know why that caused a problem, but it did.