Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

"GameManager" deleting gameComponets i add to it

Discussion in '2D' started by Grengoshi, Apr 22, 2020.

  1. Grengoshi

    Grengoshi

    Joined:
    Apr 11, 2020
    Posts:
    7
    This is my first actual "video game" so forgive me if any questions i ask in the future are really dumb.

    Issue: My "GameManager" keeps deleting the components I add to it, I do not know why it is so, but I have spent several hours trying to figure out whats going on. Again forgive the the bad code, I am still learning. I am looking forwards to an answer but if you have resources available to send me to so I cant learn a better way of doing it, Id also appreciate it.

    I put all my scripts in just in case I created some conflict between them

    AGAIN I am still learning any help would be appreciated, including advice on cleaning up my code. My one lazy mistake is not commenting the code, which i can do if you need me too. Thank you to anyone who can help me <3 (self taught :/)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7.  
    8. public class GameManager : MonoBehaviour
    9. {
    10.  
    11.     bool gameHasEnded = false;
    12.    // public float restartDelay = 1f;
    13.  
    14.     public  Canvas deathMenu;
    15.  
    16.    
    17.  
    18.     private void Start()
    19.     {
    20.         deathMenu = GetComponent<Canvas>();
    21.  
    22.         //deathMenu.enabled = false;
    23.  
    24.         Debug.Log("This is active" + deathMenu); //test if its null its how I found it was deleting compnents
    25.  
    26.      
    27.     }
    28.  
    29.     public void EndGame()
    30.     {
    31.  
    32.         if (gameHasEnded == false)
    33.         {
    34.             gameHasEnded = true;
    35.             Debug.Log("Game Over");
    36.             //Invoke("Restart", restartDelay);
    37.  
    38.             //deathMenu.SetActive(true);
    39.            
    40.  
    41.             if (Input.GetKey(KeyCode.R))
    42.             {
    43.                 Restart();
    44.             }
    45.  
    46.         }
    47.  
    48.  
    49.  
    50.      
    51.     }
    52.     void Restart()
    53.     {
    54.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    55.     }
    56.  
    57.  
    58.    
    59. }
    60.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     public float speed = 3.0f;
    9.  
    10.     public Camera cam;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.        
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         Movement();
    22.      
    23.  
    24.  
    25.     }
    26.     private void FixedUpdate()
    27.     {
    28.         RotateToMouse();
    29.     }
    30.     private void Movement()
    31.     {
    32.  
    33.         float moveX = Input.GetAxisRaw("Horizontal");
    34.  
    35.         float moveY = Input.GetAxis("Vertical");
    36.  
    37.         Vector2 movementXY = new Vector2(moveX, moveY).normalized;
    38.  
    39.         transform.Translate(movementXY * speed * Time.deltaTime);
    40.  
    41.     }
    42.  
    43.     private void RotateToMouse()
    44.     {
    45.  
    46.         Vector2 mousePos = Input.mousePosition;
    47.         Vector2 mousePosition = cam.ScreenToWorldPoint(new Vector2(mousePos.x, mousePos.y));
    48.  
    49.         // Debug.Log(mousePosition);
    50.  
    51.         Vector2 playerPosition = new Vector2(transform.position.x, transform.position.y);
    52.  
    53.         Vector2 directionVector = new Vector2(mousePosition.x - playerPosition.x, mousePosition.y - playerPosition.y) * Mathf.Rad2Deg;
    54.  
    55.         float aimAngle = Mathf.Atan2(directionVector.y, directionVector.x) * Mathf.Rad2Deg -90;
    56.         Quaternion aimRotation = Quaternion.Euler(new Vector3(0, 0, aimAngle));
    57.  
    58.         transform.rotation = aimRotation;
    59.  
    60.     }
    61.  
    62.  
    63.     private void OnCollisionEnter2D(Collision2D collision)
    64.     {
    65.         if (collision.gameObject.tag == "Enemy")
    66.         {
    67.          
    68.             //will change this to health deduction
    69.             FindObjectOfType<GameManager>().EndGame();
    70.  
    71.         }
    72.     }
    73.    
    74.  
    75.  
    76. }
    77.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.  
    8.     public Transform player;
    9.     public float moveSpeed = 5.0f;
    10.     private Rigidbody2D rb;
    11.     private Vector2 movement;
    12.  
    13.  
    14.  
    15.     void Start()
    16.     {
    17.          rb = GetComponent<Rigidbody2D>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         Vector3 direction = player.position - transform.position;//finds direction of the player by subtracting player from enemy
    24.  
    25.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;//takes the direction and calculates the angle
    26.        
    27.         rb.rotation = angle;//rotates the rb of the nemy to the player
    28.         direction.Normalize();//0-1 on angle to keep smooth
    29.         movement = direction * moveSpeed; //makes the direction var into the movement value
    30.          Vector2 moveAdjust = new Vector2(movement.magnitude, 0);
    31.  
    32.         transform.Translate(moveAdjust * Time.deltaTime);
    33.     }
    34.  
    35.     private void OnCollisionEnter2D(Collision2D collision)
    36.     {
    37.         if (collision.gameObject.tag == "Bullet")
    38.         {
    39.  
    40.             // might have to redo this, quick script idea
    41.             // Debug.Log("You hit the enemy");
    42.  
    43.             Destroy(gameObject);
    44.  
    45.  
    46.  
    47.         }
    48.        
    49.     }
    50.  
    51.    
    52. }
    53.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Bullet : MonoBehaviour
    6. {
    7.  
    8.  
    9.     public GameObject hitEffect;
    10.  
    11.     //A function for w.e we collide with something
    12.     private void OnCollisionEnter2D(Collision2D collision)
    13.     {
    14.         //damage what you hit
    15.  
    16.         GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
    17.         Destroy(effect, 5f);
    18.         Destroy(gameObject);
    19.  
    20.     }
    21.  
    22.  
    23.  
    24.  
    25.  
    26. }
    27.  
    28.  
     
  2. Grengoshi

    Grengoshi

    Joined:
    Apr 11, 2020
    Posts:
    7
    I dont know how the bumping works in this but anyone got an asnwer?
     
  3. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Don't think there is enough info here to say. What component are you adding that is being deleted? The only thing I'm seeing instantiated in the code are the bullets and effects, but neither appears to be attached to the gamemanager and I don't see any add components at all so not sure what you're doing.
     
  4. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116
    I can not see anything getting removed in the code, the only issue you might have is the scenemanager reload you are doing, did you make sure you saved your scene before playing? Else it is gonna load the the scene where you saved last..
     
  5. Grengoshi

    Grengoshi

    Joined:
    Apr 11, 2020
    Posts:
    7
    Thanks guys for the reply, i ended up scrapping everything and buil;d the game manager from scratch, but i appreciate the reply.