- Home /
'thrust' is not a member of UnityEngine.Component
Hi guys,
I have a script named Player.js with the following members:
var rollSpeed : float = 100.0; //rolling rotation speed
var pitchSpeed : float = 100.0; //vertical rotation speed
var yawSpeed : float = 100.0; //horizontal rotation speed
var accelerate : float = 20000; //main engine thrust
var velocity;
var rotSpeed = 15;
var planform;
var thrust : float = 0; var maxThrust : float = 600000;
var liftFactor : float = 0.1;
private var speed : float = 0.0;
Player.js is attached to a prefab of my player character, and controls movement. The prefab's tag is "Player".
I also have the following HUD script attached to my Main Camera:
#pragma strict
private var player : GameObject;
private var speed : float = 0;
private var thrust : float = 0;
function Start() { player = GameObject.FindWithTag("Player"); }
function OnGUI () { var groundHit : RaycastHit; Physics.Raycast( player.transform.position - Vector3.up, -Vector3.up, groundHit );
var playerScript = player.GetComponent("Player");
speed = playerScript.rigidbody.velocity.z;
thrust = playerScript.thrust;
GUI.Label( Rect( 0, 0, 256, 256 ), Mathf.Round( groundHit.distance ) + " m" );
GUI.Label( Rect( 0, 20, 128, 128 ), Mathf.Round( thrust ) + " N" );
GUI.Label( Rect( 0, 40, 128, 128 ), Mathf.Round( speed ) + " m/s" );
}
Both scripts are #pragma strict scripts. If I try to run the game in the editor, I get this error:
Assets/Entities/Player/Character/Scripts/HUD.js(21,31): BCE0019: 'thrust' is not a member of 'UnityEngine.Component'.
I can't figure it out, since I should be able to access the variable in my Player script from my HUD script this way. All help is appreciated.
MachCUBED
Answer by Eric5h5 · Mar 30, 2012 at 05:39 AM
Never use quotes in GetComponent, it's slower and makes it return Component instead of the type.
Indeed. In this case you have to explicitly cast to Player.
You did it! Removing the quotes fixed my problem, so now the HUD works as expected. Thanks!