Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Programming Error

Discussion in 'Scripting' started by Pipper, Aug 12, 2015.

  1. Pipper

    Pipper

    Joined:
    Aug 12, 2015
    Posts:
    4
    Hi my name is Jose, im From Argentina Sorry about my English :p. Im new in this world, i started with the book Learning C# by developing games with Unity 3D, and im stack here:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using Assets.Interfaces;
    4. using System.Collections;
    5.  
    6. namespace Assets.States
    7. {
    8.     public class SetupState : IStateBase
    9.     {
    10.         private StateManager manager;
    11.         private GameObject player;
    12.         private PlayerControl controller;
    13.  
    14.         public SetupState (StateManager managerRef)
    15.         {
    16.             manager = managerRef;
    17.             player = GameObject.Find("Jugador");
    18.             //Debug.Log (player);
    19.             controller = player.GetComponent<PlayerControl>();
    20.             if (Application.loadedLevelName != "Scene1")
    21.             {
    22.                 Application.LoadLevel ("Scene1");
    23.             }
    24.         }
    25.         public void StateUpdate()
    26.         {
    27.             if(Input.GetKeyUp(KeyCode.DownArrow))
    28.               {
    29.                 Debug.Log ("Estamos dentro del If de rotacion");
    30.               controller.transform.Rotate(0, controller.setupSpinSpeed * Time.deltaTime, 0);
    31.             }
    32.         }
    33.         public void ShowIt()
    34.               {
    35.  
    36.             }
    37.     }
    38. }
    39.  
    and the error:
    NullReferenceException: Object reference not set to an instance of an object
    Assets.States.SetupState..ctor (.StateManager managerRef) (at Assets/States/SetupState.cs:18)
    Assets.States.BeginState.Switch () (at Assets/States/BeginState.cs:42)
    Assets.States.BeginState.ShowIt () (at Assets/States/BeginState.cs:31)
    StateManager.OnGUI () (at Assets/Scripts/StateManager.cs:45)


    testing some thing i discover that player variable is null, but a i cant understand why, someone can help me please, Thanx.
     
    Last edited: Aug 13, 2015
  2. mholub

    mholub

    Joined:
    Oct 3, 2012
    Posts:
    123
    So you are right, it seems player variable is null. Two lines above you see player is the result of GameObject.Find() call.

    Check that in Scene there is object called Jugador and it is active, because GameObject.Find() returns only active gameobjects[/code]
     
  3. Pipper

    Pipper

    Joined:
    Aug 12, 2015
    Posts:
    4
    Yeah this is my scene1: the Object Jugador is a cube and i think is active too.
    upload_2015-8-12_17-23-12.png

    when i print player variable show me null, and when i add

    controller = player.GetComponent<PlayerControl>();

    this line make the error, i think is because the variable is null cant get the component.
     

    Attached Files:

  4. mholub

    mholub

    Joined:
    Oct 3, 2012
    Posts:
    123
    Look, you try to get the player before you load the scene.
     
  5. Pipper

    Pipper

    Joined:
    Aug 12, 2015
    Posts:
    4
    I test that but doesnt work, the Scene is previous load on BeginState:

    Code (CSharp):
    1. using UnityEngine;
    2. using Assets.Interfaces;
    3. namespace Assets.States
    4. {
    5.     public class BeginState : IStateBase
    6.     {
    7.         private StateManager manager;
    8.  
    9.         public BeginState (StateManager managerRef)
    10.         {
    11.             //Debug.Log("Dentro del Constructor de BeginState");
    12.             manager = managerRef;
    13.             if(Application.loadedLevelName != "PantallaInicio")
    14.                 Application.LoadLevel("PantallaInicio");
    15.             //Time.timeScale = 0;
    16.         }
    17.         public void StateUpdate()
    18.         {
    19.             if (Input.GetKeyUp (KeyCode.Space))
    20.             {
    21.                 //Debug.Log("Dentro del If de StateUpdate");
    22.                 //manager.SwitchState (new PlayState (manager));
    23.             }
    24.         }
    25.         public void ShowIt()
    26.         {
    27.             GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), manager.gameDataRef.beginStateSplash, ScaleMode.StretchToFill);
    28.             GUI.Box(new Rect(10,10,100,90), "Menu");
    29.             if (GUI.Button(new Rect(20, 40, 80, 20), "Jugar"))
    30.             {
    31.                 Switch ();
    32.             }
    33.             if (GUI.Button(new Rect(20, 70, 80, 20), "Opciones"))
    34.             {
    35.                 Switch ();
    36.             }
    37.         }
    38.         public void Switch()
    39.         {
    40.             //Time.timeScale = 1;
    41.    Application.LoadLevel("Scene1"); // here i load the scene
    42.             manager.SwitchState (new SetupState (manager));
    43.         }
    44.     }
    45. }
     
    Last edited: Aug 13, 2015
  6. Pipper

    Pipper

    Joined:
    Aug 12, 2015
    Posts:
    4
    Ok i found something.... if i put

    player = GameObject.Find("Jugador");
    //Debug.Log (player);
    controller = player.GetComponent<PlayerControl>();


    inside StateUpdate method it takes two or three time to get the object Jugador:

    Code (CSharp):
    1.  
    2. public void StateUpdate()
    3. {
    4. if (player != null)
    5. {
    6. player = GameObject.Find("Jugador");
    7. //Debug.Log (player);
    8. controller = player.GetComponent<PlayerControl>();
    9. }
    10. }
    11. }
    inside the Update it takes two or three times to found the player, it works but the question is why doesnt work in the construct. maybe have to make a while to wait to object to be found?

    I continue working, if someone know the answer please help me :p.
     
    Last edited: Aug 13, 2015
  7. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Please, use CODE tags when posting source codes. Makes it easier to read.