- Home /
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
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 MonoBehaviour
s, 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
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
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.
The errors are gone, but nothing works now... the game runs, but I cannot control my character, by buttons or by keys. :'(
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$$onoBehaviour
s 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.
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613134450im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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