Default Reference returning null value?

Hullo Unityers,

I’m attempting to make an ability system for my game. Basically, I have one script with a bunch of generic functions (checking if the ability is active, is it on cooldown, how many uses it has left…ect) that in turn controls a specific ability function.

Everytime the player gets a new ability, I add the specific ability script to the player. Here’s an example of an ability script…

var uses = 3;
var duration = 2.0;
var cooldown = 1.0;
var newColor = Color(0.3,0,0.3,1);
var icon : Texture;

function Activate()
{
	gameObject.GetComponent(PlayerTakeDamage).MakeInvincible();
}

function Deactivate()
{
	gameObject.GetComponent(PlayerTakeDamage).EndInvincible();
}

The open variables at the top are the values the “generic ability” script takes…right here…

function NewPower(newPower)
{
	currentPower = newPower;
	cooldown = currentPower.cooldown;
	uses = currentPower.uses;	
	duration = currentPower.duration;
	icon = currentPower.icon;
	
	Debug.Log(icon);
	
	var newColor = currentPower.newColor;
	
	GetComponent(PlayerTakeDamage).ChangeColor(newColor);
	
	GUI.GetComponent(GUIScripts).Golem(true, icon);
	
	hasPower = true;
}

This function adds a new power to the player. The newPower argument is the new specific ability script.

As you can see, I take all the open variables and assign them to private vars in this script. This works just fine…except that I want every “specific ability” script to have an icon texture (in the icon variable).

You can see I have the open variable icon as a Texture. Since the script is being assigned to the player at runtime, I set the Default Reference of the icon variable to a texture in the Inspector. So I gathered that when the script is added to the player, the icon variable SHOULD be automatically set to this texture…right? However, it’s giving me a null value instead of my texture! I don’t understand why this is happening…all the other default values work just fine, so I’m assuming it has something to do with setting the Texture as the Default Reference in the inspector.

Whew. That was kind of long winded. Anyways, hope I explained it properly…thanks for any help!

If what you do is add the component (script) to another object, I don’t think it will contain all the inspector elements (i.e.: the texture reference).

You would need to create a prefab for that.

The problem is I need to be able to add and remove scripts at runtime…is there any way I can access my texture assets through code? That way, I could just setup the texture reference in the Start() function of each script.

You could have one empty gameobject with a script that has all the usable textures and get them from there.

Or you can use Resources.Load() to get them on the fly.

Or you can get them through WWW from the Web or the filesystem on the fly.

Hmmm.

On the other hand, I could just add every single script to the player character at the beginning, allowing me to tweak the values, and just disable all the scripts, and instead of adding/removing the components I could enable/disable.

Do you know if there’s any crazy slowdown to having around 20 scripts added to a character if they’re all disabled?

Thanks for the quick answers, by the way!

Disabled scripts don’t have any performance overhead, since none of the regular update functions will be called.