Problem with calling a function from another script

Hi, guys. In my scene i’ve got a player and mob spawner. On a player i have a script(AIscript) that notifies if the mob was killed, and on the spawner i have another script(SpawnControl) that counts a number of active mobs and adds new one if the number of active is less than 10. What i want to do is to lunch a NotifyDeath() function on spawner everytime the mob is killed
AI script:

...
public GameObject spawner;
...
	void removeMe () 
{
	if (thisIsPlayer == false)
{
Instantiate(ptrScriptVariable.parAlienDeath, transform.position, Quaternion.identity );
}
	if (guard == true)
	{
		Instantiate(ptrScriptVariable.guardRagDall, transform.position, transform.rotation );
objPlayer.SendMessage("mobDied", SendMessageOptions.DontRequireReceiver);
		spawner = GameObject.Find("SpawnerControl");
		spawnerScript SpawnControl = obja.GetComponent(typeof(SpawnControl)) as SpawnControl;
		spawnerScript.NotifyDeath();
Destroy(gameObject);
		}
			
}

SpawnControl script:

...
public void NotifyDeath() {
--active;
}
...

I’m receiving this error:

Assets/Scripts/AIscript.cs(249,17): error CS0246: The type or namespace name `spawnerScript' could not be found. Are you missing a using directive or an assembly reference?

This variable declaration is backwards:
spawnerScript SpawnControl = obja.GetComponent(typeof(SpawnControl)) as SpawnControl;

Should be:

SpawnControl spawnerScript = obja.GetComponent(typeof(SpawnControl)) as SpawnControl;

Also, you might as well use generics here and avoid the runtime casting and improve type safety:

SpawnControl spawnerScript = obja.GetComponent();

Thanx for your answear, i’ve tried to change it this way but now it says:

Assets/Scripts/AIscript.cs(250,31): error CS0117: `SpawnControl' does not contain a definition for `NotifyDeath'

pretty weird, if you have NotifyDeath() in your SpawnControl script, and it’s public… should work.

yes it’s public…

Can you post both scripts in their entirety?

they are a bit… BIG.
AI script:

using UnityEngine;
using System.Collections;

public class AIscript : MonoBehaviour {
	private GameObject objPlayer;
	[B][COLOR="darkred"]public GameObject Spawner;[/COLOR][/B]
	private GameObject objCamera;
	private VariableScript ptrScriptVariable;
	
	private Vector3 inputRotation;
	private Vector3 inputMovement;
	private float upper = 0.2f;
	private float upper2 = 0.5f;
	
	public float moveSpeed = 100f;
	public float health = 50f;
	public bool thisIsPlayer;
	public float mobDied = 1;
	public bool guard = false;
	public bool iSeeYou = false;
	
	private Vector3 tempVector;
	private Vector3 tempVector2;
	
	//sounds
	public AudioClip pistolSound;
	public AudioClip shotgunSound;
	public AudioClip rifleSound;
	
	// inventory
	public float shotgunAmmo = 20;
	public float bombCount = 1;
	public float rifleAmmo = 80;
	private float shotgunEmty = 0;
	private float rifleAmmoEmpty = 0;
	public bool pistol = true;
	public bool shotGun = false;
	public bool rifle = false;
	private bool isReloading = false;

	

	void Start () {
		objPlayer = (GameObject) GameObject.FindWithTag ("Player");
		objCamera = (GameObject) GameObject.FindWithTag ("MainCamera");
		if (gameObject.tag == "Player") { thisIsPlayer = true; }
		ptrScriptVariable = (VariableScript) objPlayer.GetComponent( typeof(VariableScript) );
		
	}
	
	IEnumerator reloadTimer()
	{
		isReloading = true;
		yield return new WaitForSeconds(1);
		isReloading = false;

		
	}

	
	void Update () {
		if(guard){		
	//Vector3 fwd = transform.TransformDirection(Vector3.forward);
	transform.position = new Vector3(transform.position.x, upper2, transform.position.z);		
if (Physics.Raycast(transform.position, objPlayer.transform.position - transform.position, 20)){
iSeeYou = true;	
animation.CrossFade("walk");
//print("player ahead!");
Debug.DrawRay(transform.position, (objPlayer.transform.position - transform.position), Color.green);
}
else{
	iSeeYou = false;
	animation.CrossFade("idle");
	}
}
		
		
		if(Input.GetKeyDown(KeyCode.Alpha2)){
			shotGun = true;
			pistol = false;
			rifle = false;
			}
		if (Input.GetKeyDown(KeyCode.Alpha1)){
			shotGun = false;
			pistol = true;
			rifle = false;
			}
		if(Input.GetKeyDown(KeyCode.Alpha3)){
			shotGun = false;
			pistol = false;
			rifle = true;
			}
	
		
		if (health <= 0)
			
{
removeMe();
}
		FindInput();
		ProcessMovement();
		if (thisIsPlayer == true)
		{
			HandleCamera();
		}
	}

	void FindInput ()
	{
		if (thisIsPlayer == true)
		{
			FindPlayerInput();
		}else {
			FindAIinput();
		}
		
		if((guard==true)(iSeeYou==false))
			{
				FindAIinput();
				}
				if((guard==true)(iSeeYou==true))
				{
					FindAIinputStop();
					}
		
	}
		void FindPlayerInput ()
				{
					// find vector to move
					inputMovement = new Vector3( Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical") );
					// find vector to the mouse
					tempVector2 = new Vector3(Screen.width * 0.43f,0,Screen.height * 0.43f); tempVector = Input.mousePosition; // find the position of the moue on screen
					tempVector.z = tempVector.y;tempVector.y = 0;
					inputRotation = tempVector - tempVector2; 
					
					if (( Input.GetMouseButtonDown(0) ) (pistol))
						{
							HandleBullets();
						}	
					if ((Input.GetMouseButtonDown (0))(shotGun)!isReloading)

						{
							HandleShotGun();
						}
						
					if ((Input.GetMouseButtonDown (0))(rifle))
					{
						HandleRifle();
						}	
					}
			void HandleBullets ()
{
			tempVector = Quaternion.AngleAxis(8f, Vector3.up) * inputRotation;
			tempVector = (transform.position + (tempVector.normalized * 0.5f));
			GameObject objCreatedBullet = (GameObject) 
			Instantiate(ptrScriptVariable.objBullet, tempVector, Quaternion.LookRotation(inputRotation) ); // create a bullet, and rotate it based on the vector inputRotation
			Physics.IgnoreCollision(objCreatedBullet.collider, collider);
			audio.PlayOneShot(pistolSound);
}


			void HandleShotGun ()
{			
			tempVector = Quaternion.AngleAxis(8f, Vector3.up) * inputRotation;
			tempVector = (transform.position + (tempVector.normalized * 0.8f));
			GameObject objCreatedSGBullet = (GameObject) 
			Instantiate(ptrScriptVariable.objShotGunBullet, tempVector, Quaternion.LookRotation(inputRotation) ); // create a bullet, and rotate it based on the vector inputRotation
			Physics.IgnoreCollision(objCreatedSGBullet.collider, collider);
			audio.PlayOneShot(shotgunSound);

	 --shotgunAmmo;
	if(shotgunAmmo < 0){
		shotgunAmmo = 0;
		}
	if (shotgunAmmo == shotgunEmty){
		shotGun = false; 
		}
						StartCoroutine(reloadTimer());

	}
				void HandleRifle ()
{
			tempVector = Quaternion.AngleAxis(8f, Vector3.up) * inputRotation;
			tempVector = (transform.position + (tempVector.normalized * 0.8f));
			GameObject objCreatedSGBullet = (GameObject) 
			Instantiate(ptrScriptVariable.objRifleBullet, tempVector, Quaternion.LookRotation(inputRotation) ); // create a bullet, and rotate it based on the vector inputRotation
			Physics.IgnoreCollision(objCreatedSGBullet.collider, collider);
			audio.PlayOneShot(rifleSound);
			--rifleAmmo;
	if(rifleAmmo < 0){
		rifleAmmo = 0;
		}
	if (rifleAmmo == rifleAmmoEmpty){
		rifle = false; 
		}
	}


		
		void FindAIinput ()
		{
			inputMovement = objPlayer.transform.position - transform.position;
			inputRotation = inputMovement; // face the direction we are moving
			
		}
		void FindAIinputStop()
		{
			tempVector = rigidbody.GetPointVelocity(transform.position) * Time.deltaTime * 1000;
			inputMovement = tempVector;
			inputRotation = objPlayer.transform.position - transform.position; // facing Direction
		
		}
	void ProcessMovement()
	{
		tempVector = rigidbody.GetPointVelocity(transform.position) * Time.deltaTime * 1000;
		rigidbody.AddForce (-tempVector.x, -tempVector.y, -tempVector.z);
		rigidbody.AddForce (inputMovement.normalized * moveSpeed * Time.deltaTime);
		transform.rotation = Quaternion.LookRotation(inputRotation);
		transform.eulerAngles = new Vector3(0,transform.eulerAngles.y);
		transform.position = new Vector3(transform.position.x, upper, transform.position.z);
	}
	void HandleCamera()
	{
		objCamera.transform.position = new Vector3(transform.position.x,15,(transform.position.z)-20);
		objCamera.transform.eulerAngles = new Vector3(45,0,0);
	}
	void removeMe () 
{
	if (thisIsPlayer == false)
{
Instantiate(ptrScriptVariable.parAlienDeath, transform.position, Quaternion.identity );
}
	if (guard == true)
	{
		Instantiate(ptrScriptVariable.guardRagDall, transform.position, transform.rotation );
		objPlayer.SendMessage("mobDied", SendMessageOptions.DontRequireReceiver);
		[B][COLOR="darkred"]Spawner = GameObject.Find("SpawnerControl");
		SpawnControl spawnerScript = Spawner.GetComponent<SpawnControl>();
		spawnerScript.NotifyDeath();[/COLOR][/B]
		
Destroy(gameObject);
		}
		else {
Instantiate(ptrScriptVariable.enemyRag, transform.position, transform.rotation );
objPlayer.SendMessage("mobDied", SendMessageOptions.DontRequireReceiver);
Destroy(gameObject);
		}
	
}
}

SpawnControl script :

using UnityEngine;
using System.Collections;

public class SpawnControl : MonoBehaviour {
	
	public float spawnRate = 1.0f;
	GameObject[] enemySpawns;
	Queue waves;
	int totalWavesCount;
	int wavesCompleted = 0;
	float nextSpawnTime = 0;

	public enum GameState {
		Init,
		WaveStarting,
		Playing,
		WaveCompleted,
		GameOver,
		Victory,
	}

	GameState state = GameState.Init;

	public GameState State {
		get { return state; }
	}

	public int WavesCompleted {
		get { return wavesCompleted; }
	}

	public int TotalWavesCount {
		get { return totalWavesCount; }
	}
	
	public class Wave {
		WaveList.WaveDef waveDef;
		MobSpawner[] spawners;
		
		public MobSpawner[] Spawners {
			get { return spawners; }
		}

		public class MobSpawner {
			WaveList.WaveDef.Entry entryDef;
			int active;
			int totalSpawned;

			public MobSpawner( WaveList.WaveDef.Entry def ) {
				entryDef = def;
				totalSpawned = 0;
				active = 0;
			}

			public bool IsCompleted() {
				return ( totalSpawned == entryDef.MaxCount )  ( active == 0 );
			}

			public bool CanSpawn () {
				if( IsCompleted() )
					return false;

				if( totalSpawned >= entryDef.MaxCount )
					return false;

				if( active >= entryDef.MaxActive )
					return false;

				return true;
			}
			
			public bool Spawn (GameObject spawnPoint) {
				
				if( !CanSpawn () )
					return false;
				
				GameObject obj = Instantiate(entryDef.spawnObj, spawnPoint.transform.position, Quaternion.identity) as GameObject;
                obj.SendMessage("OnSpawn", this, SendMessageOptions.DontRequireReceiver);

				++active;
				++totalSpawned;
				return true;
			}

            [COLOR="darkred"][B]public void NotifyDeath()[/B][/COLOR] {
				--active;

				if( active < 0 ) {
					Debug.LogWarning( "MobSpawner with active < 0!" );
					active = 0;
				}
			}

		}

		public Wave (WaveList.WaveDef def) {
			waveDef = def;

			spawners = new MobSpawner[waveDef.Entries.Length];

			for( int i = 0; i < spawners.Length; ++i )
				spawners[i] = new MobSpawner( waveDef.Entries[i] );
		}

		public bool IsCompleted () {
			foreach (MobSpawner s in spawners) {
				if (!s.IsCompleted ())
					return false;
			}

			return true;
		}
	}
	

	
	void Awake() {
		
		CollectEnemySpawns ();
		
		WaveList waveList = GetComponent(typeof(WaveList)) as WaveList;
		if (!waveList)
			Debug.LogError( "GameDirector without a WaveList component!" );

		// load the wave queue
		waves = LoadWaveQueue (waveList);
		totalWavesCount = waves.Count;

		if (waves.Count < 1) {
			Debug.LogError( "Wave queue has no entries! Make sure WaveList has at least one wave entry" );
			Debug.Break();
		} else {
			Debug.Log("Queued up " + waves.Count + " waves");
		}
		
	}
	
	Queue LoadWaveQueue (WaveList list) {
		Queue q = new Queue ();

		foreach( WaveList.WaveDef waveDef in list.Waves )
			q.Enqueue (new Wave(waveDef));

		return q;
	}
	
	void CollectEnemySpawns () {
		// Find all spawn areas on the level
		enemySpawns = GameObject.FindGameObjectsWithTag("EnemySpawn");
		if (enemySpawns.Length < 1) {
			Debug.LogError("MasterSpawnControl couldn't find any EnemySpawns");
			enabled = false;
			return;
		}
		
		Debug.Log("MasterSpawnControl collected " + enemySpawns.Length + " EnemySpawns");
	}

	void FixedUpdate () {
		if( state == GameState.Init ) {
			TriggerNextWave();
		} else if( state == GameState.Playing ) {

			Wave currentWave = waves.Peek() as Wave;
			
			if( currentWave.IsCompleted() ) {
				TriggerWaveCompleted();
			} else {
				if( nextSpawnTime <= Time.time ) {
					foreach( Wave.MobSpawner spawner in currentWave.Spawners ) {
						GameObject spawnPoint = enemySpawns[Random.Range( 0, enemySpawns.Length )];
						spawner.Spawn( spawnPoint );
					}

					nextSpawnTime = Time.time + spawnRate;
				}
			}
		}
	}
	
	void TriggerNextWave () {
		if (state != GameState.Init) {
			waves.Dequeue ();
			wavesCompleted++;
		}

		if (waves.Count == 0) {
			TriggerVictory ();
			return;
		}

		state = GameState.WaveStarting;
		Invoke("TriggerWave", 3.0f);
	}
	
	void TriggerWave () {
		state = GameState.Playing;
		nextSpawnTime = Time.time;
	}
	
	void TriggerWaveCompleted () {
		Debug.Log( "Current wave completed!" );
		state = GameState.WaveCompleted;
        Invoke("TriggerNextWave", 5.0f);
	}
	
	void TriggerVictory () {
		state = GameState.Victory;
	}
	
}

It looks to me like that function is a member of MobSpawner, not SpawnControl (although it’s a little hard to tell due to the messed-up formatting).

You have a bunch of nested classes there. Your NotifyDeath method is NOT a member of SpawnControl, but rather a member of MobSpawner.

EDIT: is this level of complexity with doubly-nested classes really necessary for what you’re doing?

now i really got no idea how to access this function…

The function has to be invoked on an instance of MobSpawner, and the only instances I see are in the ‘spawners’ array, which in turn is a member of the Wave class.

I’d have to study the code pretty carefully to have an idea of what’s going on there, but in short, you have to have an instance of the MobSpawner class in order to invoke that function. What that means in practical terms depends on what exactly the code is supposed to do.

thanx for reply! i’ll try to dig it up)