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

Stuck on the GameDev.tv Unity Authorized course.

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

  1. geffpants

    geffpants

    Joined:
    Mar 26, 2020
    Posts:
    6
    Hello Everyone. I downloaded unity a week ago and bought the unity authorized gamedev.tv course on Udemy. I am on the segment (7: Spawn Manager) and on the episode (6: Stop Spawning when Player Dies). I try to run my code when he finishes the episode, but i have a unity compiler error saying that it doesn't have a definition for OnPlayerDeath. Can someone please help me figure out what's wrong? I'll link the modified code below.

    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.     private SpawnManager _spawnManager;
    17.  
    18.  
    19.     void Start()
    20.     {
    21.  
    22.         transform.position = new Vector3(0, 0, 0);
    23.         _spawnManager = GameObject.Find("Spawn_Manager").GetComponent<SpawnManager>();
    24.  
    25.         if(_spawnManager == null)
    26.         {
    27.             Debug.Log("The Spawn Manager is NULL");
    28.         }
    29.     }
    30.  
    31.  
    32.     void Update()
    33.     {
    34.         CalculateMovement();
    35.  
    36.         if (Input.GetKeyDown(KeyCode.Space) && Time.time > _canfire)
    37.         {
    38.             _canfire = Time.time + _firerate;
    39.  
    40.             Instantiate(_laserPrefab, transform.position + new Vector3(0, 0.8f, 0), Quaternion.identity);
    41.         }
    42.  
    43.  
    44.     }
    45.  
    46.     void CalculateMovement()
    47.     {
    48.         float horizontalinput = Input.GetAxis("Horizontal");
    49.         transform.Translate(Vector3.right * horizontalinput * _speed * Time.deltaTime);
    50.         float verticalinput = Input.GetAxis("Vertical");
    51.         transform.Translate(Vector3.up * verticalinput * _speed * Time.deltaTime);
    52.  
    53.  
    54.         if (transform.position.y >= 0)
    55.         {
    56.             transform.position = new Vector3(transform.position.x, 0, 0);
    57.         }
    58.         else if (transform.position.y <= -3.8f)
    59.         {
    60.             transform.position = new Vector3(transform.position.x, -3.8f, 0);
    61.         }
    62.  
    63.         if (transform.position.x > 11.3f)
    64.         {
    65.             transform.position = new Vector3(-11.3f, transform.position.y, 0);
    66.         }
    67.         else if (transform.position.x < -11.3f)
    68.         {
    69.             transform.position = new Vector3(11.3f, transform.position.y, 0);
    70.         }
    71.     }
    72.  
    73.     public void Damage()
    74.     {
    75.         _lives--;
    76.  
    77.         if (_lives < 1)
    78.         {
    79.             _spawnManager.OnPlayerDeath();
    80.             Destroy(this.gameObject);
    81.         }
    82.     }
    83. }

    SpawnManager.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnManager : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private GameObject _enemyPrefab;
    9.    [SerializeField]
    10.     private GameObject _enemyContainer;
    11.  
    12.     private bool _stopSpawning;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         StartCoroutine(SpawnRoutine());
    17.        
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.      
    24.     }
    25.  
    26.     IEnumerator SpawnRoutine()
    27.     {
    28.         while(_stopSpawning == false)
    29.         {
    30.             Vector3 postoSpawn = new Vector3(Random.Range(-8f, 8f), 7, 0);
    31.           GameObject newEnemy =  Instantiate(_enemyPrefab, postoSpawn, Quaternion.identity);
    32.             newEnemy.transform.parent = _enemyContainer.transform;
    33.             yield return new WaitForSeconds(5.0f);
    34.         }
    35.     }
    36.  
    37.     public void OnPlayerDeath()
    38.     {
    39.         _stopSpawning = true;
    40.     }
    41.  
    42.  
    43. }
    44.  
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    I can't see an error just by looking your code. You have to be more specific, namely where exactly is your error pointing to, and what exactly it says. Also it matters whether the error is a compile-error, or a run-time one.

    In the meanwhile, you could make sure that your names are typed correctly (i.e. "Spawn_Manager").
     
    matkoniecz likes this.
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Ok, you said it was a compiler error.

    What is the editor you're using?
    Most editors will show you such an error before this code is compiled, by rendering a squiggly red line beneath the unknown identifier.

    You haven't made any of the classic mistakes regarding what is happening to you, so I have to infer that you're error is in fact, outside the scope that you've described and simply precipitates into a misleading situation. And this can happen very frequently, and typically leads to headaches.

    Try to approach it holistically, and reexamine something else that could've caused such an error. Where is this other script located for example. And do you have some other errors or warnings as well?
     
    matkoniecz likes this.
  4. geffpants

    geffpants

    Joined:
    Mar 26, 2020
    Posts:
    6
    I'm using Visual Studio 2019. I have no errors (in visual studio), nor the red lines. Yes, I do have an error in the unity compiler, here's what it is:
    Assets\Scripts\Player.cs(79,27): error CS1061: 'SpawnManager' does not contain a definition for 'OnPlayerDeath' and no accessible extension method 'OnPlayerDeath' accepting a first argument of type 'SpawnManager' could be found (are you missing a using directive or an assembly reference?)
     
  5. geffpants

    geffpants

    Joined:
    Mar 26, 2020
    Posts:
    6
    I got the error in the unity console, so I don't really know if that's runtime or compiler. I'm going through and triple checking now to see if I misspelled anything.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    @geffpants I put your two scripts into separate .cs files on my system and they compile just fine.

    Maybe Visual studio lost its mind, as it sometimes does. Right click on one of the scripts and reimport it. Then try the other if that doesn't work. There is no visible typo in the code above.
     
  7. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Also:
    * Check to make sure you don't have a duplicate SpawnManager
    * Try commenting out the line "_spawnManager.OnPlayerDeath();" and seeing what happens
    * Try re-typing out the OnPlayerDeath function, and the code that is calling that function - sometimes if you copy and paste code, you can get odd characters.
     
  8. trollfoot

    trollfoot

    Joined:
    Mar 15, 2020
    Posts:
    2
    Hi @geffpants, were you able to solve this issue?

    I am running into the same problem and Visual Studio is giving me this error:

    ''Non-invocable member 'SpawnManager.OnPlayerDeath' cannot be used like a method.

    If I comment out the line it works just fine - and the tutorial seems to have it exactly the same as I do, but in the video it works ^^ Mind boggling.

    Thanks!
     
  9. trollfoot

    trollfoot

    Joined:
    Mar 15, 2020
    Posts:
    2
    Figured it out like a big boy ^^.

    I was missing:

    Code (CSharp):
    1. public void OnPlayerDeath()
    2. {
    3. _stopSpawning = true;
    4. }
    in the SpawnManager.cs

    and instead had:
    Code (CSharp):
    1.  public object OnPlayerDeath { get; internal set; }
    No idea how that happened, but in case you haven't found a fix yet - check for something similar.

    Best,
     
    Kurt-Dekker likes this.
  10. Serafeimidis

    Serafeimidis

    Joined:
    Mar 24, 2021
    Posts:
    1
    Same thing happent to me. Guess what? I had to save (CTRL + S) the scripts, so the changes would upload in Unity... Noob devs, you gotta luv us! <3
     
    orionsyndrome likes this.
  11. Kchronnos

    Kchronnos

    Joined:
    Dec 6, 2022
    Posts:
    1
    OMG just letting you know... same happens to me, and other year and other Noob.
    hehehe