How do I put a void method inside another void method?

How do I put a void method inside another void method?

I wanted to put the void OnTriggerEnter2D() method inside the void Update() method

But every time I do that, Unity understands that OnTrggerEnter2D() is not being used and it turns out that Collider doesn’t work anymore.

So how can I put one void inside another?

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class VidaPlayer : MonoBehaviour
{
    public bool Vivo = true;
    public Image lifeBar;
    public int valorAtual = 10;
    public int dano = 1;
    public bool podeAtacar = true;

    void Start()
    {
       
    }

    void Update()
    {
        void OnTriggerEnter2D(Collider2D outro)
        {  
            if(outro.gameObject.CompareTag("Dano"))
            {  
                if(podeAtacar = true && valorAtual > 0)
                {
                    valorAtual -= dano;
                    lifeBar.fillAmount = (float)valorAtual / 10;
                    StartCoroutine("TempoDeDano");
                }
                else if(valorAtual <= 0)
                {  
                    Vivo = false;
                    Destroy(gameObject);
                } 
            }
        }
    }

   

    IEnumerator TempoDeDano()
    {
        podeAtacar = false;
        yield return new WaitForSeconds(1.0f);
        podeAtacar = true;
    }
}

You probably don’t want to do that. The literal thing you’ve described is called a ‘local method’, (one function inside another) but you probably want to pass information from one method to another instead, or use a different Trigger method, like OnTriggerStay(Collider other). OnTriggerStay is called every frame that the other collider stays inside the trigger. Local functions are an advanced programming topic, but see little use in Unity.

Edit: to get better advice, try describing what you want to accomplish. “Put a void method inside another one” sounds like an attempt at a solution to a problem, so what is the actual thing you want to have happen?

1 Like

And how do I pass the information from one method to another? Could you modify the code for me to know?

Sure, the most common way is to pass information from one method to another is to use an “instance variable”. This is a variable that belongs to the script itself, not to a particular method.

public class ExampleScript : MonoBehaviour
{
    private float myFloat = 0; // This variable lives outside of any method, and can be seen from any method in this script

    private void Update()
    {
        if(myFloat > 2) // We are accessing 'myFloat', which isn't declared in this method, but we can still use it!
        {
             Debug.Log("MyFloat was bigger than 2!");
             myFloat = 0;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.tag.CompareTag("Dano"))
        {
            // You can do your own logic here, but I'm going to do something simple for the sake of the example
            myFloat += 1; // Again, using the myFloat variable from the script, just adding some value to it
        }
    }
}
1 Like

Thank you very much

Only the great xzibit has been granted that power.

^ This. You can find more info about it here.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions

Another way you might do this:

public class Example : MonoBehaviour
{
    private void Update()
    {
        ExampleOne("Hello World!");
    }

    private void ExampleOne(string customMessage)
    {
        Debug.Log(customMessage);

        ExampleTwo(200, customMessage);
    }

    private void ExampleTwo(int someNumber, string anotherMessage)
    {
        Debug.Log(someNumber);
        Debug.Log(anotherMessage);
    }

}
1 Like