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

Troubleshooting: Ray Wenderlich's tutorial "blockbuster"

Discussion in 'Scripting' started by Septic_Tank, Aug 28, 2022.

  1. Septic_Tank

    Septic_Tank

    Joined:
    Aug 28, 2022
    Posts:
    5
    I have gone through the tutorial that you can access on this link: https://www.raywenderlich.com/980-introduction-to-unity-scripting

    Most things work except the very last stage in which you add a GameController script to a game object. The problem I have is that after each restart (which occur automatically when the player dies) one more player gameobject adds to the game. This means that the player controls two balls after the first restart. After third restart it's three balls. It might even add more than one ball if more than one player game object dies during a game turn. So Something is weird with the restart code, although I copied and pasted all the codes directly from the tutorials webpage.

    Anybody have clue on how to solve this?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    What does your code look like? And did you do the part at the bottom?

    "Now return to Unity, open the Prefabs folder and change the tag of all Enemy prefabs to Enemy. Next, make the Player game object into a Prefab by dragging it into the Prefabs folder. Create an empty game object, name it GameController and attach the script you just created. Hookup all the required references in Inspector."
     
  3. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Did you correctly do the step that tells you to change the PlayerScript collidedWithEnemy to this?

    Code (CSharp):
    1. void collidedWithEnemy(Enemy enemy) {
    2.   enemy.Attack(this);
    3.   if(health <= 0) {
    4.     if(onPlayerDeath != null) {
    5.       onPlayerDeath(this);
    6.     }
    7.   }
    8. }
     
  4. Septic_Tank

    Septic_Tank

    Joined:
    Aug 28, 2022
    Posts:
    5
    Yes I did all that... very strange.
     
  5. Septic_Tank

    Septic_Tank

    Joined:
    Aug 28, 2022
    Posts:
    5
    Yes I have done that.
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Could you show your code? When you post code in the forums, make sure to use Code Tags so everyone can read it easier.
     
  7. Septic_Tank

    Septic_Tank

    Joined:
    Aug 28, 2022
    Posts:
    5
    This is the code in the "player" script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6.  
    7. public class Player : MonoBehaviour
    8. {
    9.     public event Action<Player> onPlayerDeath;
    10.  
    11.     public int health = 3;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.      
    22.     }
    23.  
    24.     void collidedWithEnemy(Enemy enemy)
    25.     {
    26.         enemy.Attack(this);
    27.         if (health <= 0)
    28.         {
    29.             if (onPlayerDeath != null)
    30.             {
    31.                 onPlayerDeath(this);
    32.             }
    33.         }
    34.     }
    35.  
    36.  
    37.     void OnCollisionEnter(Collision col)
    38.     {
    39.      
    40.  
    41.         Enemy enemy = col.collider.gameObject.GetComponent<Enemy>();
    42.         collidedWithEnemy(enemy);
    43.  
    44.         if (enemy)
    45.         {
    46.             collidedWithEnemy(enemy);
    47.         }
    48.     }
    49.  
    50. }
    51.  
    This is the code in the "GameController" script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameController : MonoBehaviour
    6. {
    7.     public EnemyProducer enemyProducer;
    8.     public GameObject playerPrefab;
    9.  
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         var player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
    15.         player.onPlayerDeath += onPlayerDeath;
    16.     }
    17.  
    18.     void onPlayerDeath(Player player)
    19.     {
    20.         enemyProducer.SpawnEnemies(false);
    21.         Destroy(player.gameObject);
    22.  
    23.         Invoke("restartGame", 3);
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.  
    30.     }
    31.  
    32.  
    33.  
    34.     void restartGame()
    35.     {
    36.         var enemies = GameObject.FindGameObjectsWithTag("Enemy");
    37.         foreach (var enemy in enemies)
    38.         {
    39.             Destroy(enemy);
    40.         }
    41.  
    42.         var playerObject = Instantiate(playerPrefab, new Vector3(0, 0.5f, 0), Quaternion.identity) as GameObject;
    43.         var cameraRig = Camera.main.GetComponent<CameraRig>();
    44.         cameraRig.target = playerObject;
    45.         enemyProducer.SpawnEnemies(true);
    46.         playerObject.GetComponent<Player>().onPlayerDeath += onPlayerDeath;
    47.     }
    48.  
    49.  
    50. }
    51.  
    52.  
     
  8. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Code (CSharp):
    1.         Enemy enemy = col.collider.gameObject.GetComponent<Enemy>();
    2.         collidedWithEnemy(enemy);
    3.         if (enemy)
    4.         {
    5.             collidedWithEnemy(enemy);
    6.         }
    You call collidedWithEnemy twice.
     
  9. Septic_Tank

    Septic_Tank

    Joined:
    Aug 28, 2022
    Posts:
    5
    Thanks a lot for your help!