Search Unity

Null reference error calling another function.

Discussion in 'Scripting' started by foxvalleysoccer, Jul 26, 2016.

  1. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    I'm attempting to call another function in another class. I'm getting a NullReferenceException: Object reference not set to an instance of an object. I read several foumn posts but was unable to put it together correctly.

    How do I properly call this function? LetsMoveThatPlayer()


    Code (CSharp):
    1.  
    2. public class SpeechGestures : Singleton<SpeechGestures>
    3. {
    4.  
    5.     public GameController myGameController;
    6.  
    7.     public GameController myscriptacceess = new GameController();
    8.  
    9.  
    10.     private void MoveUpRight(PhraseRecognizedEventArgs args)
    11.     {
    12.         //Tried this below with no luck either
    13.        // myGameController.LetsMoveThatPlayer();
    14.  
    15.         myscriptacceess.LetsMoveThatPlayer();
    16.         Debug.Log("Function");
    17.     }
    18. }
    19.  
    20. public class GameController : MonoBehaviour
    21. {
    22.     public void LetsMoveThatPlayer()
    23.     {
    24.         Debug.Log("MOVE UP RIGHT from GAmeController");
    25.     }
    26. }
    27.  
    28.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I'd recommend starting with reading the console, this will be throwing warnings along the lines of "this isn't how you create monobehaviours/components"

    Have a look at "AddComponent" https://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html if the script isn't already attached to the gameobject, or "GetComponent" https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html if it is
     
  3. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    This is my console errors. I dont understand it though. But if i remove the line of code myscriptacceess.LetsMoveThatPlayer();
    Then i get no errors.

    I added the script to The Empty game object called "GameController" in my Hierarchy. GameController is also a class. It too is added to The Empty game object called "GameController" in my Hierarchy.

    I looked through the two links you send but i dont understand how they could help me.

    NullReferenceException: Object reference not set to an instance of an object.
    at SpeechGestures.MoveUpRight(PhraseRecognizedEventArgs args)
    at SpeechGestures.KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
    at UnityEngine.Windows.Speech.PhraseRecognizer.InvokePhraseRecognizedEvent(String text, ConfidenceLevel confidence, SemanticMeaning[] semanticMeanings, Int64 phraseStartFileTime, Int64 phraseDurationTicks)
    at UnityEngine.Windows.Speech.PhraseRecognizer.$Invoke6InvokePhraseRecognizedEvent(Int64 instance, Int64* args)
    at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
     
  4. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    The problem is that you can not instantiate monobehaviours like you can with generic classes. What you want to do, is either drag a gameobject with the script attached to it to the public field in the SpeechGestures class, or have the GameController script on the same gameObject, and get it via GetComponent.

    Code (CSharp):
    1. public class sample1 : MonoBehaviour
    2. {
    3.     public GameController gameController; // drag a gameobject with this script on the public field in the inspector (see image)
    4. }
    5.  
    6. [RequireComponent(typeof(GameController))]
    7. public class sample2 : MonoBehaviour
    8. {
    9.     private GameController _gameController;
    10.  
    11.     public void Awake()
    12.     {
    13.         _gameController = GetComponent<GameController>();
    14.     }
    15. }


    My guess is that you want the first option, as I assume your GameController script is also a singleton?
     
  5. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    Solved it with this in my start function
    myGameController = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>();

    myGameController.CallThatFucntion();
     
  6. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    Thanks Timelog