How to use BroadcastMessage() for a function that was detached from the gameobject sending the message?

Viewed 27

I'm probably not doing this properly at all. I'm trying to make a script that will allow dropping and throwing of a weapon, and I'm trying to keep the script on the weapon for prefab.

  void PrepareObject() {
  
    Rigidbody gameObjectsRigidBody = Stick.AddComponent<Rigidbody>();
    gameObjectsRigidBody.mass = 0.1f;
    Debug.Log("added rigidbody to weapon");
    
  }

    public void OnThrow() {
    
      anims.SetTrigger("isThrowing");
      PrepareObject();
      StartCoroutine(LetGo());

  }

  private IEnumerator LetGo() {

    Debug.Log("let go of weapon");
    yield return new WaitForSeconds(1f);
    Hand.transform.DetachChildren();
    beingThrown = true;
    
  }

  public void OnInteract() { 

    playerInRange = Physics.CheckSphere(Stick.transform.position, interactRange, layerPlayer);
    Debug.Log("Pressed interact");
    
      if (playerInRange && beingThrown) {
        
        Debug.Log("attempted pickup");
        Stick.transform.SetParent(HandTransform);

      } else return;

    beingThrown = false;
  }

When using this code, I get OnInteract() while I'm holding the weapon, but as soon as I LetGo(), I no longer get OnInteract(). I am using the Unity Input System BroadcastMessage() on the parent.. I'm kind of lost on what to do, any suggestions?

0 Answers
Related