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 Bkenks · Jan 20, 2014 at 02:19 PM · public staticstatic script

How do I access a boolean from another class in c#?

This is my code and Im trying to access bool Press(line 3) in another script.

using UnityEngine;

public class Runner : MonoBehaviour {

 public static float distanceTraveled;
 private static int boosts;
 public static bool Press = false;

 public float acceleration;
 public Vector3 boostVelocity, jumpVelocity;
 public float gameOverY;
 
 private bool touchingPlatform;
 private Vector3 startPosition;

 void Start () {
     GameEventManager.GameStart += GameStart;
     GameEventManager.GameOver += GameOver;
     startPosition = transform.localPosition;
     renderer.enabled = false;
     rigidbody.isKinematic = true;
     enabled = false;
 }


 public static void Update () {
     if(Press == true){
         if(touchingPlatform){
             rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
             touchingPlatform = false;
         }
         else if(boosts > 0){
             rigidbody.AddForce(boostVelocity, ForceMode.VelocityChange);
             boosts -= 1;
             GUIManager.SetBoosts(boosts);
         }
     }
     distanceTraveled = transform.localPosition.x;
     GUIManager.SetDistance(distanceTraveled);
     
     if(transform.localPosition.y < gameOverY){
         GameEventManager.TriggerGameOver();
     }
 }

 void FixedUpdate () {
     if(touchingPlatform){
         rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
     }
 }

 void OnCollisionEnter () {
     touchingPlatform = true;
 }

 void OnCollisionExit () {
     touchingPlatform = false;
 }

 private void GameStart () {
     boosts = 0;
     GUIManager.SetBoosts(boosts);
     distanceTraveled = 0f;
     GUIManager.SetDistance(distanceTraveled);
     transform.localPosition = startPosition;
     renderer.enabled = true;
     rigidbody.isKinematic = false;
     enabled = true;
 }
 
 private void GameOver () {
     renderer.enabled = false;
     rigidbody.isKinematic = true;
     enabled = false;
 }
 
 public static void AddBoost(){
     boosts += 1;
     GUIManager.SetBoosts(boosts);
 }

}

And here is my other code...

using UnityEngine; using System.Collections;

public class Jump : MonoBehaviour {

 void OnMouseDown(){
     Runner.GetComponent<Runner>().Press = true;
 }

 void OnMouseUp(){
     Runner.GetComponent<Runner>().Press = false;
 }

}

Unity Keeps giving my this error:

Assets/Jump.cs(7,24): error CS0120: An object is required to access non-static member 'UnityEngine.Component.GetComponent(System.Type)'

Assets/Jump.cs(11,24): error CS0120: An object is required to access non-static member 'UnityEngine.Component.GetComponent(System.Type)'

Thanks in advanced!

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
3

Answer by Lockstep · Jan 20, 2014 at 02:30 PM

Welcome to unity answers. For the futur please do not post huge walls of code. Only post the relevant parts.

To the question: The bool press is marked as static. This means it does not belong to the instance of your runner class. It belongs to the class itself.

To access a static variable, you simply need to type Runner.Press.

The way you tried is the correct way for nonstatic variables.

I don't know how you want to use your script, but in most cases it is better not to make your variables static. If you have more than one runners in your scene the static keyword will mess stuff up, since every instance of the runner script uses the same press variable. This would case every runner to jump simultaniously.

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

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

19 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

Related Questions

NullReferenceException: on Static variables 1 Answer

Script Error 1 Answer

Public static variable not updating from void Start ()? 2 Answers

Wrong with script 1 Answer

Static member cannot be accessed 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