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 DubstepDragon · Feb 23, 2014 at 04:29 PM · c#guibuttonsstatic

Error on movement script.

I have the A and D keys set to move the player left and right. I also have on-screen buttons, doing the same thing. I have a separate script, one for the movement and some other things, and one for the on screen buttons. I am getting the following errors:

  • Assets/MoveButtons.cs(18,36): error CS0176: Static memberMovement.isMovingLeft' cannot be accessed with an instance reference, qualify it with a type name instead

  • Assets/MoveButtons.cs(22,36): error CS0176: Static memberMovement.isMovingRight' cannot be accessed with an instance reference, qualify it with a type name instead

Both my scripts are here:

MOVEMENT:

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour {
 
     public static bool IsAlive = true;
 
     public bool isMovingRight = false;
     public bool isMovingLeft = false;
 
     public static float speedVar;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Input.GetKey(KeyCode.A)) {
             isMovingLeft = true;
         } else {
             isMovingLeft = false;
         }
 
         if(Input.GetKey(KeyCode.D)) {
             isMovingRight = true;
         } else {
             isMovingRight = false;
         }
 
         //SWAGDIVIDERALLHAILLORDSWAGAXXUSPLZNOMALFURIONSPAMMANYLEGENDARYDOGECATELOGE
 
         if(isMovingLeft) {
             transform.Translate(Vector3.left * Time.deltaTime * speedVar);
         }
 
         if(isMovingRight) {
             transform.Translate(Vector3.right * Time.deltaTime * speedVar);
         }
 
         if (transform.position.x > 5) {
             transform.position = new Vector2(5, transform.position.y); // can't go further than 5
         }
 
         else if (transform.position.x < -5){
             transform.position = new Vector2(-5, transform.position.y); // can't go further than -5
         }
 
     }
 
     void OnCollisionEnter(Collision other) {
         if(other.gameObject.CompareTag("Kill")) {
             Death();
             Destroy(this.gameObject);
         }
     }
 
     void Death() {
         IsAlive = false;
         PlayerPrefs.SetInt ("Score", Score.scoreValue);
     }
 }
 

BUTTONS:

 using UnityEngine;
 using System.Collections;
 
 public class MoveButtons : MonoBehaviour {
 
     public Movement movementObject; // Drop an object that has a Movement script attached
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void OnGUI () {      
         if(GUI.Button(new Rect(Screen.width - Screen.width, Screen.height - Screen.height, 300, Screen.height), "LEFT")) {
             movementObject.isMovingLeft = true;
         }
         
         if(GUI.Button(new Rect(Screen.width - 300, Screen.height - Screen.height, 300, Screen.height), "RIGHT")) {
             movementObject.isMovingRight = true;
         }
     }
 }


Edit: Updated scripts above

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
2
Best Answer

Answer by RudyTheDev · Feb 23, 2014 at 04:37 PM

Remove static from your variables in Movement.

static means the variable is not tied to an instance of a class.

 public class Weather
 {
     public static bool isItSunny = false;
 }

means you call it with Weather.isItSunny = true;, whereas

 public class Weather
 {
     public bool isItSunny = false;
 }

means you call it with Weather localWeather = new Weather(); localWeather.isItSunny = true; (Note that you don't use new on MonoBehaviours, rather Instantiate() if you need to.)

Unless you specifically need static and know what to do with it, it is a good bet you don't need to use it. So just take out the keyword from variable declarations.

P.S. .GetComponent() is slow every time in OnGUI(), you should instead do public Movement movementObject; and you can access it directly like movementObject.isMovingLeft, without needing to call GetComponent().

Edit: clarify

Comment
Add comment · Show 14 · 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 DubstepDragon · Feb 23, 2014 at 05:12 PM 0
Share

Sorry, but I don't understand. I do not need to access isAlive, I need to access the movement bools to allow movement via interacting with the buttons in the button script.

I updated my script to the following:

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$oveButtons : $$anonymous$$onoBehaviour {
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void OnGUI () {
         $$anonymous$$ovement my$$anonymous$$ovement = new $$anonymous$$ovement();
 
         if(GUI.Button(new Rect(Screen.width - Screen.width, Screen.height - Screen.height, 300, Screen.height), "LEFT")) {
             my$$anonymous$$ovement.is$$anonymous$$ovingLeft = true;
         }
         
         if(GUI.Button(new Rect(Screen.width - 300, Screen.height - Screen.height, 300, Screen.height), "RIGHT")) {
             my$$anonymous$$ovement.is$$anonymous$$ovingRight = true;
         }
     }
 }




Two errors are showing up (again):

  • Assets/$$anonymous$$oveButtons.cs(16,36): error CS0176: Static member $$anonymous$$ovement.is$$anonymous$$ovingLeft' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d - Assets/$$anonymous$$oveButtons.cs(20,36): error CS0176: Static member $$anonymous$$ovement.is$$anonymous$$ovingRight' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d

avatar image RudyTheDev · Feb 23, 2014 at 05:50 PM 0
Share

Replace

 public static bool is$$anonymous$$ovingRight = false;
 public static bool is$$anonymous$$ovingLeft = false;

with

 public bool is$$anonymous$$ovingRight = false;
 public bool is$$anonymous$$ovingLeft = false;

isAlive was an example, I tried to explain what static does. Chances are, you don't need it.

avatar image DubstepDragon · Feb 23, 2014 at 06:27 PM 0
Share

The errors are gone, but nothing works now... the game runs, but I cannot control my character, by buttons or by keys. :'(

avatar image RudyTheDev · Feb 23, 2014 at 06:40 PM 1
Share

This is what I meant by my GetComponent P.S.:

 public class $$anonymous$$oveButtons : $$anonymous$$onoBehaviour {
      
     public $$anonymous$$ovement movementObject; // Drop an object that has a $$anonymous$$ovement script attached
      
     // Use this for initialization
     void Start () {
      
     }
      
     // Update is called once per frame
     void OnGUI () {      
         if(GUI.Button(new Rect(Screen.width - Screen.width, Screen.height - Screen.height, 300, Screen.height), "LEFT")) {
             movementObject.is$$anonymous$$ovingLeft = true;
         }
              
         if(GUI.Button(new Rect(Screen.width - 300, Screen.height - Screen.height, 300, Screen.height), "RIGHT")) {
             movementObject.is$$anonymous$$ovingRight = true;
         }
     }
 }

You never create Unity $$anonymous$$onoBehaviours with new.

P.S. I don't see any place where you set your is$$anonymous$$ovingRight/Left to false. So it will keep executing that movement forever after you first do it.

avatar image DubstepDragon · Feb 23, 2014 at 07:27 PM 0
Share

I attached the script to the main camera, added the player GameObject (the one with the movement script) and it does not work... Not only do the buttons not work, the keys do not move the player as well :'(

Can you assist me with this? $$anonymous$$y deadline for resolving this issue is tomorrow. Thanks so much for your help thus far :D

Show more comments

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

20 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

Related Questions

Problem with rect and touch 1 Answer

Distribute terrain in zones 3 Answers

Main menu help needed (C#) 2 Answers

Putting Dictionary/List using foreach as buttons in a scroll view? 3 Answers

Make a Button Behave Like a Toggle 5 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