Hi all,
I am trying to write a simple script that shoots monsters/items whenever an object of some specified class gets close. However, I am having some trouble getting my code to compile.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (CircleCollider2D))]
public class ObjectSpawner : MonoBehaviour {
public Spawnable objToSpawn;
public Vector2 spawnVelocity = Vector2.zero;
public float spawnPeriodicity = 1;
public System.Type catylist = typeof (Player);
public bool useGlobalTimer = false;
public bool useExternalActivator = false;
[HideInInspector]
public bool isActivated = false;
float timer = 0;
Collider2D other;
void Start () {
if (useExternalActivator) {
//no need for collider since activation happens elsewhere
CircleCollider2D trigger = this.GetComponent<CircleCollider2D>();
trigger.enabled = false;
}
}
void Update () {
if (!useGlobalTimer && !isActivated) {
return;
}
timer += Time.deltaTime;
if (timer > spawnPeriodicity && isActivated) {
Spawnable obj = Instantiate (objToSpawn, transform.position, transform.rotation) as Spawnable;
obj.Spawn (spawnVelocity, other);
}
timer %= spawnPeriodicity;
}
void OnTriggerEnter2D (Collider2D coll) {
//only activate if the detected object is of type catylist
if (coll.GetComponent<catylyst> () != null) { //ERROR: CATYLIST DOES NOT EXIST IN CURRENT CONTEXT
//if the object can be destroyed, we should deactivate if that happens
Damagable dmg = coll.GetComponent<Damagable>();
if (dmg != null) {
dmg.OnDeath += Deactivate;
}
isActivated = true;
other = coll;
}
}
void OnTriggerExit2D (Collider2D coll) {
//deactivate when the same instance that activated us exits the trigger
if (coll.gameObject == other.gameObject) {
Deactivate ();
}
}
void Deactivate () {
isActivated = false;
other = null;
}
}
The issue is here: coll.GetComponent<catylist> ()
. I get an error that says catylist doesn’t exist in the current context, but I don’t understand why. Catylist is of type System.Type, so shouldn’t this work?
EDIT:
So after looking around a bit I found out that template parameters have to be known at compile time, so the way I was trying to do this doesn’t work. The solution, from what I saw, is to use “reflection”, which is a concept I have never seen before.
I replaced line 41 with the following code:
System.Reflection.MethodInfo refl = GetType().GetMethod ("GetComponent").
MakeGenericMethod (new System.Type[] {catylist});
if (refl.Invoke (coll, new Object[] {}) != null) {
The code compiles, but I get a runtime error that says “Ambiguous Match Exception: Ambiguous match in method resolution”. Does anyone have an idea on how to fix that?