Search Unity

Question Destroy is not working

Discussion in 'Scripting' started by junior333istall, Apr 1, 2023.

  1. junior333istall

    junior333istall

    Joined:
    Jan 11, 2023
    Posts:
    101
    Im trying to makea pong game. Whever the score reaches 7 for one of the players, the game is over and you can restart. I'm trying to though when the game over screen pops up i don't want you to be able to move the paddles nor the ball.

    In my if statement where it says the score is greater than 7 i added a destroy function to destroy the script as it would not be needed.

    The code is not giving me any errors however when the score does reach 7 I can still move my paddle. I DONT want to move it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class PlayerScore : MonoBehaviour
    7. {
    8.     private TMP_Text _score;
    9.     private int _newScore = 0;
    10.     [SerializeField]
    11.     private GameObject _ball;
    12.     [SerializeField]
    13.     private GameObject _gameOverScreen;
    14.     private PlayerMovement _playerMovement;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         _score = GetComponent<TMP_Text>();
    20.         _playerMovement = GetComponent<PlayerMovement>();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         if ((gameObject.name == "Player1Score") && (_ball.transform.position.x <= -10) || (_ball.transform.position.y <= -7))
    27.         {
    28.             AddScore();
    29.         }
    30.         else if ((gameObject.name == "Player2Score") && (_ball.transform.position.x >= 10) || (_ball.transform.position.y >= 7))
    31.         {
    32.             AddScore();
    33.         }
    34.         if (_newScore >= 7)
    35.         {
    36.             _gameOverScreen.SetActive(true);
    37.             Destroy(_playerMovement);
    38.         }
    39.     }
    40.  
    41.     [ContextMenu("Add Score")]
    42.     private void AddScore()
    43.     {
    44.         _newScore += 1;
    45.         _score.text = _newScore.ToString();
    46.         Debug.Log("Score Added");
    47.     }
    48. }
    49.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,938
    Have you even gone as far yet as using Debug.Logs to ensure the code is even running?
     
  3. junior333istall

    junior333istall

    Joined:
    Jan 11, 2023
    Posts:
    101
    Yes, I tested it out with a debug.log. The _gameOverScreen.SetActive(true) is also working.
     
  4. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Destroy the paddle and ball…..not the script…

    Or stop it from being able to move. It looks like you’re trying to destroy the code attached to the paddle…to me anyway.
     
    angrypenguin likes this.
  5. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    You are destroying Script and not gameObject. Just replace line 37 with this

    Destroy(_playerMovement.gameObject);


    However to me make more sense just enable movement only under some condition
    if(playerScore<7>
    for example. But there is a millions of ways hot to do it.
     
    Last edited: Apr 1, 2023