• 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
0
Question by tatelax · Jan 01, 2012 at 06:29 PM · getcomponent

How do I use GetComponent to divide a variable

Like this:

 // Moved from comment
 var lavaDamage : float = 1;
 
 function OnTriggerStay (other : Collider)
 {
     var deathMenu = GetComponent(DeathMenu);
     var playerHealth = GetComponent(PlayerHealth);
     var playerMovement = GetComponent(SidescrollControl);
     if (other.CompareTag("Lava")) {
         playerHealth.health -= lavaDamage;
         playerMovement.forwardSpeed / 2;
         playerMovement.backwardSpeed / 2;
         if (playerHealth.health == 0) {
             deathMenu.reason = "You were melted in lava.";
             deathMenu.showDeathMenu = true;     
         }
     }
 }
Comment
Add comment · Show 9
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 aldonaletto · Jan 01, 2012 at 06:32 PM 0
Share

What do you mean? Separate children from the parent?

avatar image tomekkie2 · Jan 01, 2012 at 06:33 PM 0
Share

What variable of what type?

avatar image tatelax · Jan 01, 2012 at 06:41 PM 0
Share

Like this:

var lavaDamage : float = 1;

function OnTriggerStay (other : Collider) { var death$$anonymous$$enu = GetComponent(Death$$anonymous$$enu); var playerHealth = GetComponent(PlayerHealth); var player$$anonymous$$ovement = GetComponent(SidescrollControl);

 if (other.CompareTag("Lava")) {
     playerHealth.health -= lavaDamage;
     player$$anonymous$$ovement.forwardSpeed / 2;
     player$$anonymous$$ovement.backwardSpeed / 2;
     
     if (playerHealth.health == 0) {
         death$$anonymous$$enu.reason = "You were melted in lava.";
         death$$anonymous$$enu.showDeath$$anonymous$$enu = true;        
     }
 }

}

avatar image tomekkie2 · Jan 01, 2012 at 06:55 PM 0
Share

I think you should use /=2 ins$$anonymous$$d /2

avatar image tomekkie2 · Jan 01, 2012 at 07:15 PM 2
Share

but -= lavaDamage works? Have you declared the types properly? If no type could be assumed int type; if say fwdspeed = 1; then 1/2 gives 0

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jan 01, 2012 at 08:30 PM

You must specify the script reference variable types, or Unity will type them as Component and complain that health isn't part of Component or other similar things. For performance reasons, you should get the script references at Start instead of each OnTriggerStay:

var lavaDamage : float = 1; private var deathMenu: DeathMenu; // define this as a DeathMenu reference... private var playerHealth: PlayerHealth; // this as a PlayerHealth ref... private var playerMovement: SidescrollControl; // this as SidescrollControl

// get the scripts only once at Start function Start(){ deathMenu: = GetComponent(DeathMenu); playerHealth: = GetComponent(PlayerHealth); playerMovement: = GetComponent(SidescrollControl); }

function OnTriggerStay (other : Collider) { if (other.CompareTag("Lava")) { playerHealth.health -= lavaDamage; playerMovement.forwardSpeed /= 2; playerMovement.backwardSpeed /= 2;

 if (playerHealth.health == 0) {
    deathMenu.reason = "You were melted in lava.";
    deathMenu.showDeathMenu = true;     
 }

} } NOTE: The player speeds are being reduced 50% each OnTriggerStay, thus they will become virtually zero in fractions of a second. It would be better to set them to a reduced value in OnTriggerEnter and restore them in OnTriggerExit.

Comment
Add comment · 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
1

Answer by tatelax · Jan 01, 2012 at 09:18 PM

Okay, the problem was the OnTriggerStay. I used your idea aldon of just using OnTriggerEnter and OnTriggerExit. However if I were to jst use those two, it would not subtract the health like I want it to, so I used both our ideas to come up with this:

 var lavaDamage : float = 1;
 var slowSpeed = false;
 
 function OnTriggerEnter (other : Collider) {
     if (other.CompareTag("Lava")) {        
             GetComponent(SidescrollControl).forwardSpeed /= 2;
             GetComponent(SidescrollControl).backwardSpeed /= 2;    
         }
 }
 
 function OnTriggerExit (other : Collider){
     if (other.CompareTag("Lava")) {
             GetComponent(SidescrollControl).forwardSpeed *= 2;
             GetComponent(SidescrollControl).backwardSpeed *= 2;
     }
 }
 
 function OnTriggerStay() {
     var playerHealth = GetComponent(PlayerHealth);
     var deathMenu = GetComponent(DeathMenu);
     
         playerHealth.health -= lavaDamage;
 
         if (playerHealth.health == 0) {
             deathMenu.reason = "You were melted in lava.";
             deathMenu.showDeathMenu = true;        
         }
 }
Comment
Add comment · Show 4 · 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 aldonaletto · Jan 01, 2012 at 11:56 PM 0
Share

And did this work? I mean, GetComponent(SidescrollControl).forwardSpeed is working? Usually it should give an error like "forwardSpeed doesn't belong to Component", what could be fixed with another version of GetComponent:

   GetComponent.<SidescrollControl>().forwardSpeed...
avatar image tatelax · Jan 02, 2012 at 01:25 AM 0
Share

Yes it works because forwardSpeed is a variable in SidescrollControl.

avatar image Bunny83 · Jan 02, 2012 at 02:52 AM 0
Share

That's actually the part of UnityScript that i don't like. Sometimes, even when you don't use pragma strict, it complains when doing GetComponent(Type).Variable and sometimes it don't. I don't use UnityScript so i never investigated that issue.

avatar image tatelax · Jan 02, 2012 at 04:00 AM 0
Share

I plan on learning C# after this game :)

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AI Pathfinding BCE0019: 'GetComponent' is not a member of 'Object'. 1 Answer

GetComponent null reference C# 1 Answer

GetComponent of copies of one gameobject? 0 Answers

How to get script component of an object with out using its name? 2 Answers

Help with accessing scoring system variable C# plz. 3 Answers


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