Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 Cpt_Ash_Tash · Jul 12, 2016 at 07:45 PM · javascriptvariableinspectordecrease

Need health to decrease with movement in Javascript

No errors, but what I've got doesn't work and I cannot figure out why. All I'm looking for is the variable called health to go down by 1 point with every movement in the inspector. The script is attached to a player prefab. N.B. Even if I change the value of the variable in the script it remains the same in the inspector. Here's the code. Some help would be much appreciated.

pragma strict

var hit : RaycastHit;

var utran;

var newTile : GameObject;

public var health : int;

function Start () {

 health = 300;

}

function Update () {

 utran = new Vector3 (transform.position.x, transform.position.y, transform.position.z-0.5);
 
 if (Input.GetKeyUp(KeyCode.A))
 {
     if (Physics.Raycast (transform.position, -Vector3.right, hit, 25))
     {
         if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
         {
             transform.position.x -= 2.75;
             health -= 1;
         }
     }
 }
 
 if (Input.GetKeyUp(KeyCode.D))
 {
     if (Physics.Raycast (transform.position, Vector3.right, hit, 25))
     {
         if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
         {
             transform.position.x += 2.75;
             health -= 1;
         }
     }
 }
 
 if (Input.GetKeyUp(KeyCode.W))
 {
     if (Physics.Raycast (transform.position, Vector3.up, hit, 25))
     {
         if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
         {
             transform.position.y += 2.75;
             health -= 1;
         }
     }
 }
 
 if (Input.GetKeyUp(KeyCode.S))
 {
     if (Physics.Raycast (transform.position, -Vector3.up, hit, 25))
     {
         if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
         {
             transform.position.y -= 2.75;
             health -= 1;
         }
     }
 }
 
 
 if (Physics.Raycast (utran, Vector3.forward, hit, 5))
 {
     if (hit.transform.tag == "Exit")
     {
         Application.LoadLevel(Application.loadedLevel);
     }
 }
 
 if (Physics.Raycast (utran, Vector3.forward, hit, 5))
 {
     if (hit.transform.tag == "Tassel")
     {
         Destroy (hit.transform.gameObject);
         Instantiate (newTile, hit.transform.position, hit.transform.rotation);
     }
 }
 

}

Comment
Add comment · Show 5
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 ahaykal · Jul 13, 2016 at 07:08 AM 0
Share

I am not sure if I understood the question quite well.

You want your health to decrease with every movement?

If so, then start debugging your code, make sure it is going inside physics.raycasthit.

Also decrease your health not inside Physics.raycasthit, decrease it on keydown.

Eg:

 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S))
  {
 health -= 1;
      if (Physics.Raycast (transform.position, -Vector3.up, hit, 25))
      {
          if (hit.transform.tag == "Floor" || hit.transform.tag == "Exit" || hit.transform.tag == "Tassel")
          {
              transform.position.y -= 2.75;
              
          }
      }
  }
avatar image rmassanet · Jul 13, 2016 at 10:00 AM 0
Share

What do you mean by "not working"? Is the character not moving? Is its health not decreasing? Are you detecting collisions?

avatar image Cpt_Ash_Tash · Jul 13, 2016 at 10:22 AM 0
Share

To answer rmassanet, the variable and its value appear in the inspector, the problem is that that value should go down with each movement. The RayCast works fine as the character collides with what it is meant to, the script literally does everything it should except when the character moves the value in the inspector doesn't decrease like it should.

To answer ahaykal, yeah, so the player should hit the key, the character moves, and the health variable in the inspector decreases by one. Debugged the code, the raycasting works fine. Tried changing the placing of 'health -= 1;' and it had no effect.

avatar image Cpt_Ash_Tash · Jul 13, 2016 at 10:30 AM 0
Share

ahaykal, I did that and it is decreasing the health with every movement just like I wanted it to, it didn't even occur to me that the inspector wouldn't be updating properly, thank you

avatar image ahaykal Cpt_Ash_Tash · Jul 13, 2016 at 10:48 AM 0
Share

Glad it worked out! Can you please accept the answer!

1 Reply

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

Answer by ahaykal · Jul 13, 2016 at 10:26 AM

Can you please print health instead of checking it out in the inspector, sometimes it doesn't update properly.

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

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to create a script dinamic like animation>size controling the Elements variables? 1 Answer

Javascript and C#, different behaviour in inspector ? 1 Answer

Why is my class hidden in inspector with C# and not JS? 1 Answer

Reading variable from other script 1 Answer

What is typed variable? 2 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