Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Amitr · Sep 27, 2014 at 01:19 PM · managerstate

State problem from tutorial

Hi all. I have asked a qustion about this chapter from the book before, but this time its a different one.

Im working with the book "Learning C# by developing games with unity 3D beginners guid"

I have finished and understand the state chapter 8, and for some reason when i just did simple test it dose not work. I need help.

Problem is: Play State (2ed state) dose not show update running and update dose not respone.

Log show me:

Constructing BeginState

BeginState Update is running

Constructing PlayState (after i press space)

press space again or any other key dose not give me the scene asked for or the beginstate again.

no "PlayState Update is running" on console.

Please explain me I am fighting with this for days.

Thanks !

code:


namespace Assets.Code.Interfaces { public interface IStateBase

{ void StateUpdate (); void ShowIt (); }

}



using UnityEngine; using Assets.Code.States; using Assets.Code.Interfaces;

public class StateManager : MonoBehaviour {

 private IStateBase activeStae;

 private static StateManager instanceRef;

 void Awake ()
 {
     if (instanceRef == null)
     
     {
         instanceRef = this;
     DontDestroyOnLoad (gameObject);

     }

     else 
     {
     DestroyImmediate (gameObject);

     }


 }


     void Start ()


     {

     activeStae = new BeginState (this);
     }
 


public void Update () {

     if (activeStae != null) 
         activeStae.StateUpdate ();
             
 
 }

 void OnGUI()

 {
     if (activeStae!=null)
         activeStae.ShowIt();
 }

 public void SwitchState (IStateBase newState)

 {
     newState = activeStae;

 }

}



using UnityEngine; using Assets.Code.Interfaces;

namespace Assets.Code.States { public class BeginState : IStateBase

{ private StateManager manager;

         public BeginState (StateManager managerRef)

     {
         manager = managerRef;
         Debug.Log ("Constructing BeginState");
     }



     public void StateUpdate()
     {
         Debug.Log ("BeginState Update is running");
         if (Input.GetKeyUp (KeyCode.Space))

         {
             Switch();
         }

     }


     public void ShowIt()

     {


     }

     void Switch ()

     {
         Application.LoadLevel ("Scene2");
         manager.SwitchState (new PlayState (manager));

     }


 }

}



using UnityEngine; using Assets.Code.Interfaces;

namespace Assets.Code.States { public class PlayState : IStateBase {

     private StateManager manager;

 // Use this for initialization
     public PlayState (StateManager managerRef)
         
     {
         manager = managerRef;
         Debug.Log ("Constructing PlayState");
     }

     public void StateUpdate()
     {
         Debug.Log ("PlayState Update is running");
         if (Input.GetKeyUp (KeyCode.Space))
             
         {
             Switch();
         }
         
     }
     
     
     public void ShowIt()
         
     {
         
         
     }
     
     void Switch ()
         
     {
         Application.LoadLevel ("Scene3");
 
         
     }
     
     
 }
 

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by gjf · Sep 27, 2014 at 01:30 PM

your formatting leaves a lot to be desired but the problem is most likely in line 17 of StateManager.cs

 newState = activeStae;

should be

 activeStae = newState;


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Amitr · Sep 27, 2014 at 01:47 PM

Thank you, that solve it. I am now continue to try and connect Touch script to this system so a touch button will affect it.

I am trieng to add a standart touch script that loop thru touches and check if "began" phase has been made.

I dont really sure how to integrate in into this system

Can i create a reference for the button (GUItexture) it self on the state and put the call for that reference in StateUpdate? checking whatever if this button has been press, if so call a method on the button to Switch state \ load scene ? is this a good planning?

thanks !

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Amitr · Sep 27, 2014 at 03:23 PM 0
Share

I have made this now, but for some reason after i press in my android the "PlayGameButton" which is GUItexture, i get this error and the game pause:

"NullReferenceException: Object reference not set to an instance of an object Assets.Code.States.TeamSelectionState..ctor (.State$$anonymous$$anager managerRef) (at Assets/Code/States/TeamSelectionState.cs:19) Assets.Code.States.PlayGameButtonClass.Press () (at Assets/Code/Buttons/PlayGameButtonClass.cs:46) Assets.Code.States.$$anonymous$$ain$$anonymous$$enuState.StateUpdate () (at Assets/Code/States/$$anonymous$$ain$$anonymous$$enuState.cs:36) State$$anonymous$$anager.Update () (at Assets/Code/Scripts/State$$anonymous$$anager.cs:40)"

Those are the States involved:


using UnityEngine; using Assets.Code.States; using Assets.Code.Interfaces;

public class State$$anonymous$$anager : $$anonymous$$onoBehaviour {

 private IStateBase activeState;

 private static State$$anonymous$$anager instanceRef;

 void awake ()
 {
     Debug.Log ("Awake");
             if (instanceRef == null) 
         {
         Debug.Log ("instanceRef = this");
                 instanceRef = this;
         DontDestroyOnLoad(gameObject);
         } 
         else 
         {
         Debug.Log ("Destroied Gameobject");
             DestroyImmediate (gameObject);
         }

 }
 
 void Start ()

     {
             activeState = new $$anonymous$$ain$$anonymous$$enuState (this);
     }

 void Update ()

     {
             if (activeState != null)
                     activeState.StateUpdate ();
     }
 

 void OnGUI () 

 {
     if (activeState !=null)
     activeState.ShowIt();

 }


 public void SwitchState (IStateBase newState)
     
 {
     activeState = newState;
 }

}



using UnityEngine; //using System.Collections; //using System.Collections.Generic; using Assets.Code.Interfaces;

namespace Assets.Code.States { public class $$anonymous$$ain$$anonymous$$enuState : IStateBase {

     CustomizeButtonClass CustomizeRef;
 //    OptionsButtonClass     OptionsRef;
     PlayGameButtonClass PlayGameRef;

     private State$$anonymous$$anager manager;



     public $$anonymous$$ain$$anonymous$$enuState (State$$anonymous$$anager managerRef)
     
         
     {    
         manager = managerRef;
         Debug.Log ("Constructing $$anonymous$$ain$$anonymous$$enuState");    
         CustomizeRef = GameObject.Find ("CustomizeButton").GetComponent<CustomizeButtonClass>();
     //    OptionsRef = GameObject.Find ("OptionsButton").GetComponent<OptionsButtonClass>();
         PlayGameRef = GameObject.Find ("PlayGameButton").GetComponent<PlayGameButtonClass>();

                                     
     }
 

     public void StateUpdate () 
         
     {
         PlayGameRef.Press ();

         CustomizeRef.Press ();

 //        OptionsRef.Press ();
      
     
     }

     public void Switch ()

     {

     }

 
 
     
     public void ShowIt ()
         
     {
 

         
     }

 

 
     
 }

}


using UnityEngine; using System.Collections; using System.Collections.Generic; using Assets.Code.Interfaces;

namespace Assets.Code.States { public class TeamSelectionState : IStateBase { private State$$anonymous$$anager manager; public ChasersSelectionButtonClass ChasersSelectionButtonClassRef;



avatar image Amitr · Sep 27, 2014 at 03:23 PM 0
Share

is the Script that is attached to the GUItexture which is the PlayGameButton

using UnityEngine; using System.Collections; using System.Collections.Generic; using Assets.Code.States;

namespace Assets.Code.States {

 public class PlayGameButtonClass : $$anonymous$$onoBehaviour
 {

          

     private State$$anonymous$$anager manager;

 void Start ()

     {
         manager = GameObject.Find ("Game$$anonymous$$anager").GetComponent<State$$anonymous$$anager> ();
     }

     public void Press ()
         
     {

         if (Input.touches.Length < 0) 
             
         {
             Debug.Log ("Touch the buttons retard !");
         } 
         
         else 
             
         {
             // check how many touches 
             for (int i = 0; i < Input.touchCount; i++)
                 
             {
                 
                 if(this.guiTexture.HitTest(Input.GetTouch (i).position))
                     
                     
                 {
                     if (Input.GetTouch(i).phase == TouchPhase.Began)
                     {        
                         manager.SwitchState (new TeamSelectionState (manager));

                             
                     }

                 }
             }
         }
 
 }

} } I am really stack. Why can't it create a reference?

Thanks !

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Game Manager - simply use static variables? 0 Answers

Storing and Removing GameObjects - List or Dictionary? 2 Answers

How to store data for a custom "Manager" 1 Answer

error CS0117: `Util' does not contain a definition for `SetLayerRecursively 0 Answers

Item Registry System 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges