How to check if array member is a specific type [solved]

Nevermind, found the way to do it…I always spend hours searching the forum for the solution, don’t find it, post a message, and then give the search one more try and immediately find my answer:

http://forum.unity3d.com/viewtopic.php?t=26244&highlight=object+type

Hi!

Here’s my issue.

I have an array[ ]. The members are mixed between integers, GameObjects, Scripts, etc

let’s say that:

array[0] = 1;
array[1] = null;
array[2] = GameObject;
array[3] = myScriptinstance;
array[4] = null;
array[2] = GameObject;
array[3] = myScriptdifferentinstance;

I’ve tried to simply loop through the array and use the following to check if the member is a script or a GameObject:

if (array == myScript)
//do stuff
or
if (array == GameObject)
//do stuff
but it always returns false…
Help Please :slight_smile:
Thanks!
Jason

This is bad programming practice, you should not keep an array of different types of objects if you need to do run-time checking on them. Store them in seperate structures instead.

Of course, it’s not impossible to do.

This seemed the most efficient method given the actual usage.

Here’s what I’m doing, let me know if you think I should still try to do it differently. I am definately a coding novice.

I have 8 different arrays(arrayCombatSession00 - 07). Each represents a potential “combat session” between 1-4 attackers and 1-4 defenders. There may be up to 8 different sessions between different attackers/defenders.

each arrayCombatSession contains 20 fields

0 - # of attackers
1 - # of defenders
2 - primary attackerGO
3 - primary defenderGO
4/5 - 10/11 : attacker1GO/attacker1Script - attacker4GO/attacker4Script
12/13 - 18/19 : as above but defenders

attackers and defenders may come and go/die during each combat round.

This make sense? Better way to do it?

Thanks!

Jason

Just an example. You can extend it for your own purposes.

class CombatSession{
	public var primaryAttacker : AttackerScript;
	public var primaryDefender: DefenderScript;
	
	private var attackers : ArrayList;
	private var defenders : ArrayList;
	
	public function CombatSession(){
		attackers = new ArrayList();
		defenders = new ArrayList();
	}
	
	/**
	 * returns number of Attackers
	 */
	public function AttackerCount(): int{
		return attackers.Count;
	}
	
	/**
	 * Add an Attacker to this Combat. isPrimary designates the 
	 */
	public function AddAttacker(attacker : AttackerScript, isPrimary : boolean){
		attackers.Add(attacker);
		if(isPrimary)
			primaryAttacker = attacker;
	}
	/**
	 * Remove an Attacker
	 */
	public function RemoveAttacker(attacker : AttackerScript){
		if(primaryAttacker == attacker){
			primaryAttacker = null;
		}
		attackers.Remove(attacker);
	}
	
	public function GetAttackerScript(index : int): AttackerScript{
		if(attackers.Count - 1 < index || index < 0 ){
			Debug.LogError("Requested Attacker that does not exist");
			return;
		}
		return attackers[index];
	}
	
	public function GetAttackerObject(index : int): GameObject{
		if(attackers.Count - 1 < index || index < 0 ){
			Debug.LogError("Requested Attacker that does not exist");
			return;
		}
		return attackers[index].gameObject;
	}
	
	// As Above for defenders.
}
/**
 EXAMPLE USE OF CombatSession
 */
 
var combatSession01 : CombatSession;

combatSession01.AddAttacker(someAttacker, false);
combatSession01.AddAttacker(otherAttacker, true);

Debug.Log(combatSession01.primaryAttacker.name); // "otherAttacker"
Debug.Log(combatSession01.DefenderCount()); // "0"
Debug.Log(combatSession01.GetAttackerScript(0).name); // "someAttacker"

Horsman,

Thanks for the input. This is something i will definitely consider to make the code more functional :slight_smile:

I love this forum!

Jason