Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Abblot · Nov 02, 2015 at 03:44 PM · buttonmenuboolean

Boolean not functioning as intended.

Okay so I'm working on a simple first person game and I'm currently in the process of making a start menu for the game.

I've made the menu and everything, but the problem comes when my "Play"-button is supposed to make the player movements available by making the "running" boolean that I made "true".

Here's the code for the player controller:

 public class FirstPersonController : MonoBehaviour {
 
     public float movementSpeed = 5;
     public float rotateSpeed = 3;
 
     public bool running = false;
 
     float rotZ = 0;
 
     public float zRange = 60;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
         if (running == true)
         {
             //Mouse
             Cursor.lockState = CursorLockMode.Locked;
             //Cursor.visible = false;
 
 
             //Rotation
             float rotX = Input.GetAxis("Mouse X") * rotateSpeed;
 
             rotZ -= Input.GetAxis("Mouse Y") * rotateSpeed;
             rotZ = Mathf.Clamp(rotZ, -zRange, zRange);
             Camera.main.transform.localRotation = Quaternion.Euler(rotZ, 0, 0);
 
             transform.Rotate(0, rotX, 0);
 
             //Movement
             float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
             float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
 
             Vector3 speed = new Vector3(sideSpeed, 0, forwardSpeed);
 
             speed = transform.rotation * speed;
 
             CharacterController cc = GetComponent<CharacterController>();
 
             cc.SimpleMove(speed);
         }
 
 
     }
 }
 

And here's the code for the menu:

 public class menuScript : MonoBehaviour {
 
     public Canvas quitMenu;
     public Button startText;
     public Button exitText;
 
     FirstPersonController fpcScript;
 
     GameObject fpcGameObject;
 
     // Use this for initialization
     void Start () {
 
         quitMenu = quitMenu.GetComponent<Canvas>();
         startText = startText.GetComponent<Button>();
         exitText = exitText.GetComponent<Button>();
         fpcScript = fpcGameObject.GetComponent<FirstPersonController>();
 
         quitMenu.enabled = false;
 
     }
     
     public void ExitPress(){
 
         quitMenu.enabled = true;
         startText.enabled = false;
         exitText.enabled = false;
 
     }
 
     public void NoPress(){
 
         quitMenu.enabled = false;
         startText.enabled = true;
         exitText.enabled = true;
 
     }
 
     public void StartLevel(){
 
         fpcScript.running = true;
 
     }
 
     public void ExitGame(){
 
         Application.Quit();
 
     }
 
 }

Would greatly appreciate some help with this. I've been stuck with this for a few hours now and I just can't get it to work.

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

1 Reply

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

Answer by Statement · Nov 02, 2015 at 03:52 PM

Both of these fields are private, so they cant be set from inspector.

 FirstPersonController fpcScript; 
 GameObject fpcGameObject;

In start you try to get the FirstPersonController from an uninitialized reference.

 fpcScript = fpcGameObject.GetComponent<FirstPersonController>();

This will fail and you should see an error in the console. As a result, fpcScript will remain null.

Then you are trying to set the running boolean on nothing.

 public void StartLevel() {
     fpcScript.running = true; // Note! fpcScript is null! 
 }

So the problem isn't with your boolean. The problem is that you don't have a reference to the controller.

Fix it by providing a reference to the game object that has the script.

 public GameObject fpcGameObject; // now it's public, set it up in the inspector.
Comment
Add comment · Show 1 · 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 Abblot · Nov 02, 2015 at 08:42 PM 0
Share

Aaah, took me a while to figure what you said out, but it works perfectly now! Well explained, since even I got it after some thinking :) Thanks for the help!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I create a static bool? 1 Answer

Problem with enabling menu 0 Answers

activate button on triggerenter 0 Answers

How to get access to bool from one script to another with touch input? 1 Answer

How to make pie menu options interact to OnMouseDown? 0 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