Boost Pad Script error - "not a member of 'UnityEngine.

I’m trying to implement a boost pad into a space shooter, and I’m scratching my head on what I’m doing wrong here.

The elements are:
A ship
A boost pad

The ship is controllable on its own, but when it hits a target I want a temporary pickup in speed.

Using different examples from the forums, I came up with this and attached it to my Boost Pad (a GO with a box collider set to Trigger).

var boostPad : GameObject; 
var ship : GameObject;
var target : Transform; 
var boostSpeed = 1000; 


//Sets up the Booster function
function Booster() {
	    if( ! target ) 
        return; 

    var dir=(target.position - transform.position).normalized; 
    transform.position += dir * boostSpeed * Time.deltaTime; 
}    

//Calls the Booster function when triggered
function OnTriggerEnter(col : Collider) { 
      
      ship.Booster();
      Destroy (boostPad.gameObject); 
 
 }

The error is in the “ship.Booster()” line.

"Booster is not a member of ‘Unity.GameObject.’ I’m confused because I thought this error happened when something wasn’t defined. Am I not defining “function Booster” correctly?

I tested this by just placing the Booster function on the Ship itself and it works fine as a “function Update” but obviously that won’t let me use a trigger.

I also tried splitting the script between the Boost Pad and the Ship, still with the same error. For this, I used the SuperJump example from the ThirdPersonController script as an example, where the function SuperJump is defined in the TPC script and then called by the Jumppad script.

Combining them into one script was an attempt to narrow down the problem, but I’m stuck…

:frowning:

Any help is greatly appreciated.

(Also, I realize that once the Ship arrives at the target location it will be stuck at that spot, so the next step after fixing my current problem is to destroy the target location upon arrival - but that part I think I have covered).

Here it is split as two scripts.

Boost.js attached to the Ship.

var target : Transform; 
var boostSpeed = 1000; 

function Booster() {
	    if( ! target ) 
        return; 

    var dir=(target.position - transform.position).normalized; 
    transform.position += dir * boostSpeed * Time.deltaTime; 
}

And BoostPads.js attached to the Boost Pad.

var boostPad : GameObject; 
var ship : GameObject;

function OnTriggerEnter(col : Collider) { 
      ship.Booster();
      Destroy (boostPad.gameObject); 
 
 }

Still, with the same error. Ideas?

You have to get a reference to the script first before you can call a function in it, by using GetComponent or whatever.

–Eric

Ah, gotcha.

I’m trying to do it without GetComponent because ultimately I’d like to put this on the iPhone, and I thought GetComponent was off limits, due to dynamic typing.

I thought I could just reference the GO that the code is attached to as in this set of Checkpoint scripts that work perfectly:

Checkpoint.js attached to the Checkpoint:

var checkpoint : GameObject; 
var score : Score; 

function OnTriggerEnter(col : Collider) { 
   
     score.AddToScore(); 
     Destroy (checkpoint.gameObject); 
    
}

So… if I’m understanding it correctly, this script calls the function AddToScore, which is located in Score.js, which is attached to a GUIText object called GUIScore:

var score = 0;

function Start () { 
     guiText.text = score.ToString(); 
} 

function AddToScore () { 
    score++; 
    guiText.text = score.ToString(); 
}

In this example the " var score : Score; " is set to the GUIScore object, which contains the script that defines function AddToScore.

This all works without a GetComponent, so using this same model I thought I could build the Boost Pads in the same way.

However, I did notice that in the Jumppad example from the 3rd Person game, it seems to point to the ThirdPersonController script from within Jumppad.js, when defining “controller.” which in my case would be “ship”:

var jumpHeight = 5.0;

function OnTriggerEnter (col : Collider) 
{
	var controller : ThirdPersonController = col.GetComponent(ThirdPersonController);
	if (controller != null)
	{
		if (audio) 
		{
			audio.Play();
		}

		controller.SuperJump(jumpHeight);
	}
	
}


// Auto setup the script and associated trigger.
function Reset ()
{
	if (collider == null)	
		gameObject.AddComponent(BoxCollider);
	collider.isTrigger = true;
}

@script RequireComponent(BoxCollider)
@script AddComponentMenu("Third Person Props/Jump pad")

Would this type of GetComponent be considered dynamic typing? If so, is there a better way to accomplish what I’m trying to do?

Thanks for your help. I’m learning. Slowly, but I’m learning!

:?

GetComponent doesn’t have anything to do with dynamic typing, fortunately…it would be impossible to do much of anything without it. :wink: For example, this is dynamic:

GetComponent(ParticleEmitter).emit = true;

This isn’t:

var pe : ParticleEmitter = GetComponent(ParticleEmitter);
pe.emit = true;

Actually it doesn’t:

guiText.text = score.ToString();

is really a shortcut (without dynamic typing) for

GetComponent(GUIText).text = score.ToString();

Just put “#pragma strict” at the top of all your scripts, and it will disallow any dynamic typing.

–Eric

That’s great! I was going around and around trying to figure out how to do the most basic things without using GetComponent, completely wrong about what dynamic typing means and not realizing that

guiText.text = score.ToString();

is a static form of GetComponent. (Which explains why the Checkpoint/Score script works at all). Nice.

So… Lot’s to learn, but understanding this one concept has answered a lot of questions regarding other aspects of my game.

This has been a huge help. Thanks Eric.

Ideally what you’d do is something like:

function OnTriggerEnter( o : Collision ) {
   var booster = o.gameobject.GetComponentInChildren("Booster");
   if ( booster ){
      booster.Boost();
   }
}

(This is from memory so it may be horribly wrong somewhere.)

Note: this requires the boostpad to have a collider attached which is set to act as a trigger.

Also note: for bonus points you can use @script RequireComponent(SphereCollider) and when you attach this script to something it automatically gets a SphereCollider – and then you can put something in the Awake() or Start() functions to set the attached collider to target.

Voila – virtually a magic script. Drag it onto something and it Just Works.

Now you don’t need to drag your ship to each BoostPad – it will find the appropriate script automagically AND the BoostPad will work for any game entity that has a booster (e.g. for multiple players or enemies that can use boosters too).

@podperson

Interesting… I like the magic script approach. :smile: