The member `Clock.creaturesArray' cannot be used as method or delegate

I was looking around and couldn’t find an answer to what the above error means. I am trying to send a message in a script to the first object in an array and I get this error at the send message. Here’s the script.

using UnityEngine;
using System.Collections;

public class Clock : MonoBehaviour {

	public int hour;
	public int minute;
	public int second;
	public bool PM;
	public int n;
	public ArrayList playerCreatures;
	public object[] creaturesArray;
	
	// Use this for initialization
	void Start () {
	
		hour = 12;
		minute = 0;
		PM = false;
		second= 0;
		n=1;
		playerCreatures.Add(GameObject.Find("Bullark_Elec_v1"));
		creaturesArray = playerCreatures.ToArray();
		
	}
	
	// Update is called once per frame
	void Update () {
	
		if ( Time.time >= second){
			second ++;
			minute++;	
			
		}
		
		if ( minute == 60){
			hour++;
			minute = 0;	
			
		}
		if (hour > 23){
		hour = 0;	
			
		}
	
		if ( second == n*4){
			
			
			creaturesArray(1).SendMessage(Stamina, hour);
			
			n++;
			
		}
	}
}

thanks for an explanation!

1 Answer

1

First of your creatureArray is of type "object". An Object doesn't have a SendMessage function. It should be of type GameObject since SendMessage is a function of GameObject.

Your actual problem is that you used round brackets which are only used for methods. You need square brackets `[]`.

creaturesArray[1].SendMessage(Stamina, hour);

Keep in mind that index 1 is the second object in the array. 0 is the first one. Also don't forget if you try to use an index that doesn't exist you'll get a runtime error.

edit

Well, another problem is this line:

creaturesArray = playerCreatures.ToArray();

because you use the ArrayList. The ArrayList class stores untyped objects. You better use a generic list instead:

public List<GameObject> playerCreatures;

Don't forget to include this namespace:

using System.Collections.Generic;

I don't get why you even use a containerclass. It contains one element, so you can just put it into the array.

In Start() you can do this:

creaturesArray = new GameObject[] { GameObject.Find("Bullark_Elec_v1") };

or

creaturesArray = new GameObject[1];
creaturesArray[0] = GameObject.Find("Bullark_Elec_v1");

If you plan to store more than one object, you can just use the List<>. Why do you even use an array? ;)

i keep getting this error though when i put it as a GameObject. I think thats why i changed it to object Cannot implicitly convert type object[]' to UnityEngine.GameObject[]'. An explicit conversion exists (are you missing a cast?)

I've edited my answer ;)

Thanks that worked! i do want to add more creatures eventually thats why i needed a list :)