Scaling a GameObject via script

Halfway there but needing a stopping point…

Here’s what I want:

  • Clicking on a button sends a message to an object that is intended to increase the size of the object.
if (selGridIntPol == 0)	{
    		BroadcastMessage ("ShowCapitols");
}
  • The object that is supposed to increase has this code …
function ShowCapitols () {
	transform.localScale += Vector3(0.1,0.1,0);
}

Now, this “works” (no error messages or anything) … BUT the object (a sphere) continues to increase in size without stopping! Until it fills the entire screen!

Ideal would be for it to go from it’s default scale (0.2, 0.2, 0.2) to a new scale (0.3, 0.3, 0.3) and then stop, remaining at the new scale until receiving a message to return to it’s original size.

Any thoughts?

So, your results would indicate that ShowCapitols() is being called repetitively. Assuming the code you show is the only place that calls ShowCapitols, then I’d guess it’s firing repetitively. The question is why? You don’t show what method that code is in, nor what sets the selGridIntPol variable. So, it’s a little hard to guess what might be wrong. Do you just need to unset setlGridIntPol until something else triggers it again?

Jeff

The default view looks like this:

1493617--83543--$scaleTest1.png

See the blue dot surrounded by white dots? That’s where the capitol is located. Clicking the button should cause it to look like this:

1493617--83544--$scaleTest2.png

See how the blue dot grew relative to the white ones? That’s what I’m going for.

I’m thinking you’re probably right that it’s firing repetitively, but if so it’s the only button/toolbar button/selection grid button I’ve had do that - almost like it’s acting as a Repeat button.

Can you post the entire function containing the first snippet you posted? Since you don’t show any context with that snippet, it’s difficult to guess what’s going on.

Sure…

function Political (windowID : int) {
	selGridIntPol = GUI.SelectionGrid (new Rect (5, 25, 400, 100), selGridIntPol, selPol, 4);
    	if (selGridIntPol == 0)	{
    		BroadcastMessage ("ShowCapitols");
    	}
        if (selGridIntPol == 1)	{
		BroadcastMessage ("ShowAmbassadors", SendMessageOptions.DontRequireReceiver);
	}
	if (selGridIntPol == 2)	{
		BroadcastMessage ("ShowIntel");
	}
	if (selGridIntPol == 3)	{
		BroadcastMessage ("ShowPopulation");
	}
	if (selGridIntPol == 4)	{
		BroadcastMessage ("ShowSupport");
	}
	if (selGridIntPol == 5)	{
		BroadcastMessage ("ShowExplorers");
	}
	if (selGridIntPol == 6)	{
		BroadcastMessage ("ShowStates");
	}
	if (selGridIntPol == 7)	{
		BroadcastMessage ("ShowColonies");
	}
GUI.DragWindow (Rect (0,0,10000,10000));
}

I’m certain that using regular buttons rather than a Selection Grid would solve the problem I’m just hoping there’s an easier way

I’m not sure about the Selection Grid, but I think once you’ve selected it, it stays at that number, and OnGUI() will check it every frame. If so, you can easily check it by doing this:

if (selGridIntPol == 0) {
BroadcastMessage ("ShowCapitols");
selGridIntPol = -1 //this might work, not sure
}

See if that works, in that you’d have to click it again to scale the capitol again.

The other (better) option is to have another variable as a flag, which says “capitol hasn’t been scaled yet”, then once it’s scaled, set that to true so it won’t happen repeatedly. BTW all your other messages will broadcast continuously as well, but if there’s no visual feedback, that might not be evident. Use print() to see what’s going on.