Search Unity

delegate void GeneralEvent error, why?

Discussion in 'Scripting' started by grimofdoom, Jan 23, 2016.

  1. grimofdoom

    grimofdoom

    Joined:
    Sep 6, 2013
    Posts:
    148
    I am going through a really good series in youtube for learning Unity and C# programming, and I have made mistakes in the past videos that I was able to fix. This one though is not a mistake from what I can tell (by re-writing the scripting with the video 5 times to all fail). I just do not understand it and I do not want to just skip this video about event handling, because it feels like I will miss something important and useful.

    I recieve the error "error CS20029: Cannot implicitly convert type 'void' to 'GameManager_EventMaster.GeneralEvent' ". I understand this error means that these two are not two types that can intermingle because they are not the same type.(It throws the error on 'Spawner.cs')



    Code (CSharp):
    1. //GameManager_EventMaster.cs
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class GameManager_EventMaster : MonoBehaviour {
    6.  
    7.     public delegate void GeneralEvent();
    8.     public event GeneralEvent myGeneralEvent;
    9.  
    10.  
    11.     public void CallMyGeneralEvent(){
    12.         if (myGeneralEvent != null) {
    13.             myGeneralEvent ();
    14.         }
    15.     }
    16.  
    17.  
    18.     /*void Start () {
    19.  
    20.     }
    21.  
    22.     void Update () {
    23.  
    24.     }*/
    25. }
    26.  
    Code (CSharp):
    1. //spawner.cs
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Spawner : MonoBehaviour {
    6.  
    7.     public GameObject Enemy;
    8.     public int numberOfEnemies;
    9.     private float spawnRadius = 5;
    10.     private Vector3 spawnPosition;
    11.     private GameManager_EventMaster eventMasterScript;
    12.  
    13.     void OnEnable(){
    14.         SetReferences ();
    15.         eventMasterScript.myGeneralEvent += SpawnEnemies();
    16.     }
    17.  
    18.     void OnDisable(){
    19.         eventMasterScript.myGeneralEvent -= SpawnEnemies();
    20.     }
    21.  
    22.     void SetReferences(){
    23.         eventMasterScript = GameObject.Find("GameManager").GetComponent<GameManager_EventMaster>();
    24.     }
    25.  
    26.     void SpawnEnemies(){
    27.         for (int i = 0; i < numberOfEnemies; i++) {
    28.             spawnPosition = transform.position + Random.insideUnitSphere * spawnRadius;
    29.             Instantiate (Enemy, spawnPosition, Quaternion.identity);
    30.         }
    31.     }
    32. }
    33.  
    Code (CSharp):
    1. //TriggerExample.cs
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TriggerExample : MonoBehaviour {
    6.  
    7.     private GameManager_EventMaster eventMasterScript;
    8.     private WalkThroughWall walkThroughWallScript;
    9.  
    10.     void OnTriggerEnter (Collider other) {
    11.         //walkThroughWallScript.SetLayerToNotSolid ();
    12.         eventMasterScript.CallMyGeneralEvent ();
    13.     }
    14.  
    15.     void OnTriggerExit (Collider other) {
    16.         //walkThroughWallScript.SetLayerToDefault ();
    17.     }
    18.  
    19.     void SetReferences(){
    20.         eventMasterScript = GameObject.Find("GameManager").GetComponent<GameManager_EventMaster>();
    21.         if(GameObject.Find("Wall") != null){
    22.             walkThroughWallScript = GameObject.Find ("Wall").GetComponent<WalkThroughWall>();
    23.         }
    24.     }
    25.  
    26.     void Start(){
    27.         SetReferences ();
    28.     }
    29.        
    30. }
    31.