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 /
avatar image
0
Question by B0DP9I40K · Aug 22, 2017 at 01:49 PM · scripting problemnoobpublic static

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!

Comment
Add comment · Show 2
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 tanoshimi · Aug 22, 2017 at 01:54 PM 0
Share

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.

avatar image B0DP9I40K tanoshimi · Aug 22, 2017 at 02:11 PM 0
Share

i already found a solution, but thanks, maybe it will help me later

2 Replies

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

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.

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
avatar image
0

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.

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

115 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 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 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 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 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 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

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


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