Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by DEBEST69 · Jun 03, 2021 at 10:33 AM · scripting problemcollisiongameobjectsrestart game

I got a bunch of problems and i don't know how to fix them!

I'm making a game where i have to protect a base from being destroyed. However i have problems with the rest of the code what i mean is they don't work! One, the enemies won't collide with the base which is supposed to make them disappear and take one of the base's lives each. Two, GameOver text won't appear when the lives are gone and three the keycode to restart the game does not work either. Is there a way all this can be fixed?

 public class GameManager : MonoBehaviour
 {
     public bool gameHasEnded = false;
     public string gameState = "Play";
     public bool isplayable = true;
     public GameObject GameOver;
     public GameObject refToBase, refToPlayer, refToEnemy, refToEnemy2, refToCannon;
     public GameObject Lives, Lives2, Lives3;
     public int LifeNum;
     // Start is called before the first frame update
     void Start()
     {
         GameOver.gameObject.SetActive(false);
         LifeNum = 3;
         Lives.gameObject.SetActive(true);
         Lives2.gameObject.SetActive(true);
         Lives3.gameObject.SetActive(true);
         refToCannon = GameObject.Find("Cannon");
     }
 
     // Update is called once per frame
     void Update()
     {
         if (LifeNum > 3)
         {
             LifeNum = 3;
         }
 
         switch (LifeNum)
         {
             case 3:
                 Lives.gameObject.SetActive(true);
                 Lives2.gameObject.SetActive(true);
                 Lives3.gameObject.SetActive(true);
                 break;
             case 2:
                 Lives.gameObject.SetActive(true);
                 Lives2.gameObject.SetActive(true);
                 Lives3.gameObject.SetActive(false);
                 break;
             case 1:
                 Lives.gameObject.SetActive(true);
                 Lives2.gameObject.SetActive(false);
                 Lives3.gameObject.SetActive(false);
                 break;
             case 0:
                 Lives.gameObject.SetActive(false);
                 Lives2.gameObject.SetActive(false);
                 Lives3.gameObject.SetActive(false);
                 gameHasEnded = true;
                 Time.timeScale = 0;
                 break;
         }
 
         if (gameHasEnded == true)
         {
             gameState = "Stop";
             refToPlayer.GetComponent<Player>().PlayerState = "Stop";
             refToCannon.GetComponent<Player>().PlayerState = "Stop";
             isplayable = false;
             GameOver.gameObject.SetActive(true);
 
             if (Input.GetKey(KeyCode.R))
             {
                 Restartgame();
             }
         }
     }
     void Restartgame()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
     }
 }



 public class Enemy2 : MonoBehaviour
 {
     public GameObject refToBase;
     public GameObject refToGameManager;
     public float moveSpeed;
     public string Enemystate;
     private Transform target;
     private int health = 1;
     // Start is called before the first frame update
     void Start()
     {
         moveSpeed = 0.6f;
         target = GameObject.FindGameObjectWithTag("Threat").GetComponent<Transform>();
     }
 
     // Update is called once per frame
     void Update()
     {
         transform.position = Vector2.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
     }
 
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.CompareTag("Cannonball"))
         {
             Destroy(collision.gameObject);
             health--;
             if (health <= 0)
             {
                 Death();
             }
         }
 
         if (collision.CompareTag("Threat"))
         {
             Destroy(this.gameObject);
             refToGameManager.GetComponent<GameManager>().LifeNum -= 1;
         }
     }
 
     private void Death()
     {
         Destroy(this.gameObject);
         
     }


 public class Base : MonoBehaviour
 {
     public GameObject refToGameManager;
     public GameObject refToCannonball;
     // Start is called before the first frame update
     void Start()
     {
        
     }
 
     // Update is called once per frame
     void Update()
     {
        
         if (this.GetComponent<SpriteRenderer>().bounds.Intersects(refToCannonball.GetComponent<SpriteRenderer>().bounds))
         {
             Destroy(gameObject);
         }
     }
 
     private void OnTriggerEnter2D (Collider2D collision)
     {
         if (collision.gameObject.tag.Equals ("Enemy"))
         {
             Destroy(gameObject);
             refToGameManager.GetComponent<GameManager>().LifeNum -= 1;
         }
     }
 }

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

0 Replies

· Add your reply
  • Sort: 

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

314 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 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 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 collectable objects don't respond like they should after calling a coroutine for the first time? 0 Answers

[Unity 2D]Grid Based Character Movement Issues with Vector3D.movetowards 0 Answers

i want game to end on no collisions 0 Answers

Get player and enemy hit bars to respond to each others attack not their own 1 Answer

How to detect if not colliding 1 Answer


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