Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Can't assign a method as a callback in the Editor

Discussion in 'Scripting' started by jnbbender, May 29, 2019.

  1. jnbbender

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    366
    I've done this a million times but just not sure why this time it isn't working. I have an OnEvent trigger attached to my character and a separate GameObject with a script called WeaponHandler.cs. This script has a method called IsArmed(). Shown below.

    Code (CSharp):
    1.  
    2. public bool IsArmed()
    3. {
    4.     return isArmed;
    5. }
    The WeaponHandler script inherits from MonoBehaviour.

    When I attach the GameObject to the event, the WeaponHandler script shows as an option but does not display its "IsArmed()" method.

    I have attached a picture below to try and explain better.
     

    Attached Files:

  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    8,546
    You're sure your project has no console errors preventing the script from compiling? If no errors, can you post the whole script?
     
  3. jnbbender

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    366
    Yep, no errors. I can run without any issues. Here's the WHOLE script.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Invector.vShooter;
    6.  
    7. public class WeaponHandler : MonoBehaviour
    8. {
    9.     public bool IsArmed() {
    10.         return transform.childCount > 0;
    11.     }
    12. }
    13.  
     
  4. jnbbender

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    366
    Okay, here's am odd one. I added a new "Disarm" method and THAT shows up.

    Code (CSharp):
    1.  
    2. public class WeaponHandler : MonoBehaviour
    3. {
    4.     public Animator animator;
    5.  
    6.     public bool IsArmed() {
    7.         return transform.childCount > 0;
    8.     }
    9.  
    10.     public void Disarm() {
    11.         if (IsArmed()) {
    12.             animator.SetTrigger("EquipItem");
    13.         }
    14.     }
    15. }
    16.  
     
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    998
    Correct me if I'm wrong, but UnityEvents can only call methods that return void. That's why your Disarm() method shows up, but not your IsArmed() method.
     
    Joe-Censored likes this.
  6. jnbbender

    jnbbender

    Joined:
    May 25, 2017
    Posts:
    366
    Yeah, I feel pretty dumb now. Thanks for the input
     
unityunity