- Home /
a problem with a public static bool in very very simple 2d game
hello guys, sorry for my english(i live somewhere in post ussr ), i am almost total n00b in programming and i wanted to create my very first game just to share it with my friends. This is a pretty simple 2d android game where bombs are falling at the player and player needs to move in x axis to avoid them( i am doing it by a guide). So, the problem is that i was doing actions in case of losing, and i have already done script file which determines the losing itself
public class player : MonoBehaviour {
public static bool lose = false;
void OnTriggerEnter2D (Collider2D other) {
if (other.gameObject.tag == "enemy")
lose = true;
}
}
then i inserted the "while (!player.lose)" before the bombs spawning script so the bombs stop spawning after you lose and it worked fine
using System.Collections;
using UnityEngine;
public class SpawnEnemies : MonoBehaviour {
public GameObject enemy;
void Start () {
StartCoroutine (Spawn ());
}
IEnumerator Spawn () {
while (!player.lose) {
Instantiate (enemy, new Vector2 (Random.Range (-2.4f, 2.4f), 5.79f ), Quaternion.identity );
yield return new WaitForSeconds (1.5f);
}
}
}
and then i tried to do the same action with moveplayer script because i wanted that you won't be able to move player anymore after losing but it failed with error "Assets/scripts/moveplayer.cs(12,14): error CS1061: Type UnityEngine Transform does not contain a definition for lose and no extension method lose of type UnityEngine Transform could be found. Are you missing an assembly reference?"| the script moveplayer itself looked like this:
using UnityEngine;
public class moveplayer : MonoBehaviour {
public Transform player;
[SerializeField]
private float speed = 10f;
void OnMouseDrag () {
if(!player.lose) {
Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
mousePos.x = mousePos.x > 2.4f ? 2.4f : mousePos.x;
mousePos.x = mousePos.x < -2.4f ? -2.4f : mousePos.x;
player.position = Vector2.MoveTowards (player.position,
new Vector2 (mousePos.x, player.position.y),
speed * Time.deltaTime);
}
}
}
sorry if it's stupid problem but as i said i am total newbie and i did'nt find answers on the internet... so how i can make it work fine? thanks!
Rule 1: never use the static
modifier unless you're absolutely sure you know what it does. I suggest you remove it and learn to use GetComponent.
i already found a solution, but thanks, maybe it will help me later
Answer by SilverSho0t · Aug 22, 2017 at 02:19 PM
I men, this code don't work because you search lose in player but player is a Transform variable not your script of class 'player' so you need to call this script :
if (!player.GetComponent<Player>().lose)
or use a variable of class Player which assign your script directly
public Player player;
also set the first letter of your class name with a capital letter to not confuse them with a variable name like i do just above 'player' 'Player'
So your three script corrected :
using UnityEngine;
public class Player : MonoBehaviour {
public bool lose = false;
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.tag == "enemy")
lose = true;
}
}
using UnityEngine;
using System.Collections;
public class SpawnEnemies : MonoBehaviour {
public GameObject enemy;
public Player player;
void Start ()
{
StartCoroutine (Spawn ());
}
IEnumerator Spawn ()
{
while (!player.lose)
{
Instantiate (enemy, new Vector2 (Random.Range (-2.4f, 2.4f), 5.79f ), Quaternion.identity);
yield return new WaitForSeconds (1.5f);
}
}
}
using UnityEngine;
public class Moveplayer : MonoBehaviour {
[SerializeField]
private float speed = 10f;
public Transform player;
void OnMouseDrag ()
{
if (!player.GetComponent<Player>().lose)
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
mousePos.x = mousePos.x > 2.4f ? 2.4f : mousePos.x;
mousePos.x = mousePos.x < -2.4f ? -2.4f : mousePos.x;
player.position = Vector2.MoveTowards (player.position,
new Vector2 (mousePos.x, player.position.y),
speed * Time.deltaTime);
}
}
}
I don't use a static variable in class Player you don't need it, it's cleaner like this.
I hope I help, Sean.
Answer by B0DP9I40K · Aug 22, 2017 at 02:10 PM
oh, actually when you carefully describe your problem it makes you think better. So the trouble was about lowercase & uppercase letters, because in moveplayer script i already had a define for player.
Your answer
![](https://koobas.hobune.stream/wayback/20220612133303im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My Animation Loops 3/4 of the way through 3 Answers
How to detect which how many of a type a variable a script has 1 Answer
How do I reset my object rotation when it collides and spins around 1 Answer
BEGINNER: why is my main menu not loading level 1 when I press play? 1 Answer
How can I make the player stop moving when a UI dialogue box is active? 2 Answers