• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by MachCUBED · Mar 30, 2012 at 04:18 AM · getcomponentcomponenthud

'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 t$$anonymous$$s 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 t$$anonymous$$s way. All help is appreciated.

MachCUBED

Comment
LeonardChallis

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment
PushpaK
BiG
LeonardChallis
NirmalNishanth

People who like this

2 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kryptos · Mar 30, 2012 at 08:00 AM 0
Share

Indeed. In this case you have to explicitly cast to Player.

avatar image MachCUBED · Mar 30, 2012 at 05:03 PM 0
Share

You did it! Removing the quotes fixed my problem, so now the HUD works as expected. Thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Reference Not Set toInstance of Object 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Using GetComponent in multiple scripts for same component? C# 2 Answers

Change the Halo Component (not the default) in a light in C#??? 0 Answers

Destroy(GetComponent(...)) not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges