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

Confused about the Gamdev.tv Unity authorised course.

Discussion in 'Scripting' started by geffpants, Apr 4, 2020.

  1. geffpants

    geffpants

    Joined:
    Mar 26, 2020
    Posts:
    6
    I am taking the gamedev.tv Unity Authorised course when I get to the section "Enemies" and the episode number is "8. Player Lives and Damaging", and in that episode, he codes in the lives function, and what's meant to happen is if you crash into the enemy 3 times, the player object gets destroyed. But when I run it (I checked the code), it doesn't reduce my lives? I'll link the code modified in that episode below, please help.

    Player.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private float _speed = 3.5f;
    9.     [SerializeField]
    10.     private GameObject _laserPrefab;
    11.     [SerializeField]
    12.     private float _firerate = 0.5f;
    13.     private float _canfire = 1f;
    14.     [SerializeField]
    15.     private int _lives = 3;
    16.  
    17.  
    18.     void Start()
    19.     {
    20.  
    21.         transform.position = new Vector3(0, 0, 0);
    22.     }
    23.  
    24.  
    25.     void Update()
    26.     {
    27.         CalculateMovement();
    28.  
    29.         if (Input.GetKeyDown(KeyCode.Space) && Time.time > _canfire)
    30.         {
    31.             _canfire = Time.time + _firerate;
    32.  
    33.             Instantiate(_laserPrefab, transform.position + new Vector3(0, 0.8f, 0), Quaternion.identity);
    34.         }
    35.  
    36.  
    37.     }
    38.  
    39.     void CalculateMovement()
    40.     {
    41.         float horizontalinput = Input.GetAxis("Horizontal");
    42.         transform.Translate(Vector3.right * horizontalinput * _speed * Time.deltaTime);
    43.         float verticalinput = Input.GetAxis("Vertical");
    44.         transform.Translate(Vector3.up * verticalinput * _speed * Time.deltaTime);
    45.  
    46.  
    47.         if (transform.position.y >= 0)
    48.         {
    49.             transform.position = new Vector3(transform.position.x, 0, 0);
    50.         }
    51.         else if (transform.position.y <= -3.8f)
    52.         {
    53.             transform.position = new Vector3(transform.position.x, -3.8f, 0);
    54.         }
    55.  
    56.         if (transform.position.x > 11.3f)
    57.         {
    58.             transform.position = new Vector3(-11.3f, transform.position.y, 0);
    59.         }
    60.         else if (transform.position.x < -11.3f)
    61.         {
    62.             transform.position = new Vector3(11.3f, transform.position.y, 0);
    63.         }
    64.     }
    65.  
    66.     public void Damage()
    67.     {
    68.         _lives--;
    69.  
    70.         if (_lives < 1)
    71.         {
    72.             Destroy(this.gameObject);
    73.         }
    74.     }
    75. }
    Enemy.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     [SerializeField]
    9.     private float _speed = 4.0f;
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         transform.Translate(Vector3.down * _speed * Time.deltaTime);
    19.  
    20.         if (transform.position.y < -5f)
    21.         {
    22.             float randomX = Random.Range(-8f, 8f);
    23.             transform.position = new Vector3(randomX, 7, 0);
    24.         }
    25.     }
    26.  
    27.     private void OnTriggerEnter(Collider other)
    28.     {
    29.         if(other.tag == "Player")
    30.         {
    31.             Player player = other.transform.GetComponent<Player>();
    32.  
    33.             if(player != null)
    34.             {
    35.                 player.Damage();  
    36.             }
    37.  
    38.            
    39.             Destroy(this.gameObject);
    40.         }
    41.  
    42.         if (other.tag == "Laser")
    43.         {
    44.             Destroy(other.gameObject);
    45.             Destroy(this.gameObject);
    46.         }
    47.     }
    48.  
    49.  
    50. }
    51.  
    52.  
     
  2. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    Does your player object has a tag? Put some Debug.log() inside enemy script trigger to if the collision are raised correctly
     
  3. panim0_unity

    panim0_unity

    Joined:
    Nov 13, 2018
    Posts:
    47
    I've just tried your code and it works. make sure you player obj has tag yes and rigidbody component.
     
    geffpants likes this.
  4. geffpants

    geffpants

    Joined:
    Mar 26, 2020
    Posts:
    6
    thanks dude, im quite new to this.