Invocation Exception while setting alpha?

I’m tired, stupid, or both.

I have a regular GameObject, with a material using Alpha/Diffuse as shader. The texture assigned has an alpha channel. The Color of the material is set to a color:

markerobject.renderer.material.color=Color.green;

Works fine. However:

markerobject.renderer.material.color.a=0.5;

Throws a TargetInvocationException:

“Exception has been thrown by the target of an invocation”.

Any ideas? Many thanks for any help!
Cheers,
Dan

EDIT:
And weirdly enough, repeating the first statement above (…color=Color.green; ) in place of the second statement (…color.a=0.5; ) throws the same error.
The functions are in the same script, and markerobject is a private var which isn’t touched between the two events (as far as I can tell).

So if you just write two lines like these

markerobject.renderer.material.color=Color.green;

one after the other, you get the error?

What if you get the material, and then assign color color, like this:

var mat = markerobject.renderer.material;
mat.color = Color.green;

No, the weird part is that everything works (including setting alpha) in the Start part of the script.

Later on, another function (Mark) in the same script is called, and that’s when the exception is thrown:

var shipmarkerRes : GameObject;
var shipIlluRes : guiIconButton;

private var shipIllu : guiIconButton;
private var markerobject : GameObject;

private var gameControl: GameControl;


// FUNCTIONS ------------------------------------------


function Awake () {

}

function Start () {
	//print(Resources.Load("Prefabs/shipmarker",GameObject));
	var ttrans= this.transform.position + Vector3(0,0.1,0);
	
	markerobject = Instantiate(shipmarkerRes,ttrans,this.transform.rotation);
	markerobject.transform.parent=this.transform;
	
	markerobject.renderer.material.color=Color.green;
	markerobject.renderer.material.color.a=0.2;
	print("OK this far!");	
		
	// TEMP
	var obj = Resources.Load("gui/ShipCommand");
	var tgobj = Instantiate(obj,Vector3(0,0,0),Quaternion.identity);
	commandObject = tgobj.GetComponent("ShipCommand");
	commandObject.SetParent(this);
}

function Update () {
	
	
}

function SetPlayer(reflink : Player) {
	playerReference = reflink;	
}



function OnMouseUp() {
	//check if I can activate UI
	if (playerReference.isCommandActive() == 1  gameControl.GetInputState()==0) {
		//build UI
		commandObject.DisplayIcons();

		}	
	else
	{
		gameControl.ReportClick("ship",this);
	}

}


function Mark () {
	  markerobject.renderer.material.color=Color.green; // THROWS EXCEPTION!
}

I have stripped LOTS of code from the above, so ignore other stuff that doesn’t make sense. The point is I can’t for the life of me understand why the statements work in the Start function, but not later on. I can’t see any changes between the calls.
Any ideas where to look?

Thanks! :slight_smile:

Well, I don’t know what happens between Start and the invocation of your function. I’d start with something like this in Mark function:

print(markerobject);
print(markerobject.renderer);
print(markerobject.renderer.material);
print(markerobject.renderer.material.color);

And see which of these (if any) do “fail”, i.e. print not what you think should be printed.

Nothing changes between them, actually. And the exception was thrown immediately when I tried to access .renderer in any way.

However, I solved it purely by chance. At least I think I did.

In another script (the one that called the “problematic one” quoted above), I had mistyped a variable (not in any way connected to this problem). That didn’t in itself cause an error however.

I set aside this problem to work on other stuff while waiting for a reply, and stumbled upon the misspelling when troubleshooting another routine. After fixing it, this problem (in another script) seems to have vanished.

So, it would seem to me that a relatively “innocent” error managed to bypass error detection and snowball into a major error elsewhere. Is that normal in Unity…?

Many thanks for your help! :slight_smile:
Cheers,
Dan

Its a misunderstood error location. The problem didn’t actually occur where you were thinking it was happening, instead, it was happening in the other script which caused the program to fail later. I realize this doesn’t make much sense, it didn’t to me at first either, but in my game code, if I have bad logic in one script that calls another script with sendmessage, the error fell to the next script that I was calling, and it had nothing to do with the actual error.

When all else fails, look at the calling methods and scripts for logical errors there.

Makes sense. I’m familiar with the concept (been coding games in other languages for eons), but what threw me was that Unity had caught all my errors (in their original “places”) up until this point. I think I’ll have to be a bit more careful in the future now… :slight_smile:

Anyway, thanks for the help guys - and have a good weekend! :slight_smile:

Cheers,
Dan