Using a variable to define a getComponent type

Hey guys, I am having trouble getting this section of code to work with #pragma.

#pragma strict

var Damage : int = 1;
var Duration : float = 0.5;
var Effect : String = "none"; //maybe for fire resistance
var objScrpt : String = "";

var timer : float = 0.0;


function FixedUpdate () {
	timer += Time.deltaTime;
	
	if(timer > Duration){
		if(objScrpt != ""){
			[B](transform.parent.gameObject.GetComponent(objScrpt) as objScrpt)[/B].dmgspawnReady = true;
		}
		Destroy(gameObject);
	}
}

I bolded the section that has the problem. Basically, it gets the name of the script from another object and sets it as a string, which I use to tell the GetComponent which script to run. However, this runs without #pragma, but seeing as this is for an iphone game that will not do. How can I define a variable as the type cast? Thanks

This might work…

var objScript : Component;
(transform.parent.gameObject.GetComponent(objScrpt) as objScript).dmgspawnReady = true;

Nope, same error:

Thank you though

In C#, you would explicitly reference a “type” (read: script definition) using “typeof”

var objScript : Type;

objScript = typeof(MyTargetScript); // notice the lack of quotation marks around MyTargetScript

and… wait a sec

You’re trying to access dmgspawnReady on your objScript? That means you know the type already. Also, the way you’re casting (“as objScrpt”) doesn’t make sense, it needs to be pointing to a compile-time-type, not a string.

Honestly, I don’t even know how this runs/compiles with OR without #pragma.

I’m assuming all the possible script’s you’re going to be using here all have the “dmgspawnReady” property? If so, then you’ll need to employ polymorphism (base classes and/or interfaces). (or worse, reflection, but I don’t know if that’s supported on iPhone or not) I’m not entirely sure on the JavaScript syntax for it, so I’ll just throw out the C# syntax and hopefully you can get an idea of what I’m getting at.

public interface IDamageHandler
{
	bool dmgspawnReady;
}

public class SomeDamageHandlerImplementation : MonoBehavior, IDamageHandler
{
	public bool dmgspawnReady;
}

public class SomeOtherDamageHandlerImplementation : MonoBehavior, IDamageHandler
{
	public bool dmgspawnReady;
}


public class damageScrpt : MonoBehavior
{
	public Type objScrpt;

	public void FixedUpdate()
	{
		if(objScrpt != null)
		{
			IDamageHandler damageHandler = transform.parent.gameObject.GetComponent(objScrpt) as IDamageHandler;
			if (damageHandler != null)
			{
				damageHandler.dmgspawnReady = true;
			}
		}
		Destroy(gameObject);
	}
}

EDIT:

Then to set the “objScrpt” reference:

//in some other script somewhere
damageScrpt target = GetMyDamageScript(); //your implementation
target.objScrpt = typeof(SomeDamageHandlerImplementation);
//or
target.objScrpt = typeof(SomeOtherDamageHandlerImplementation);

Come to think of it, I think for JS, you don’t need the “typeof()” call, I think you can just use the script name without qutoes (but I’m not sure on that).

Thanks for your post, I had a hard time understanding it (I am not a coder), but it gave me an idea on how to do it and it worked:

#pragma strict

var Damage : int = 1;
var Duration : float = 0.5;
var Effect : String = "none"; //maybe for fire resistance
var objScrpt : String = "";

var timer : float = 0.0;


function FixedUpdate () {
	timer += Time.deltaTime;
	
	if(timer > Duration){
		if(objScrpt == "BladeTrapscrpt"){
			BladeTrp();
		}else if(objScrpt == "FireTrapscrpt"){
			FireTrp();
		}else if(objScrpt == "SpikeTrapscrpt"){
			SpikeTrp();
		}
		Destroy(gameObject);
	}
}

function BladeTrp(){
	(transform.parent.gameObject.GetComponent("BladeTrapscrpt") as BladeTrapscrpt).dmgspawnReady = true;
}

function FireTrp(){
	(transform.parent.gameObject.GetComponent("FireTrapscrpt") as FireTrapscrpt).dmgspawnReady = true;
}

function SpikeTrp(){
	(transform.parent.gameObject.GetComponent("SpikeTrapscrpt") as SpikeTrapscrpt).dmgspawnReady = true;
}

Thanks again :slight_smile:

Hey there you go, that’s not too bad for a quick solution either!

Just if I could give you one piece of advice, take the time to fully name your variables and apply proper casing. I had a hell of a time trying to keep that “i” out of “objScrpt/dmgScrpt/etc”. So:

objScrpt → objScript (or maybe objectScript, or even targetScript)
BladeTrp() → BladeTrap()
FireTrapscrpt → FireTrapScript

Minor things like this can make a world of difference in the long run. :smile: