Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Chris498 · Oct 15, 2013 at 07:47 PM · errorsyntax

Load gameoverscreen after condition. Error

Hi, I will just get straight to the point. I want my gameoverscreen to load after the player hits 20 health or less, this is my code:

 function gameover()
 {
    if ( PlayerHealth <= 20 )
    Application.LoadLevel("Gameoverscreen");
 }

Every time I get this error: Operator '<=' cannot be used with a left hand side of type 'System.Type' and a right hand side of type 'int'.

I hope you guys can help me out :)

-Chris

Comment
Add comment · Show 7
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 meat5000 ♦ · Oct 15, 2013 at 07:50 PM 0
Share

Out of curiosity, how did you declare PlayerHealth?

avatar image Chris498 · Oct 15, 2013 at 08:23 PM 0
Share

Since I am fairly new to the whole Unity and scripting thing. I don't know what you mean by that :( I am making a slender game ( Yes, I know. ) by using the guide of AlucardJ. His code set the var from 0 to 100, if that is what you mean.

avatar image meat5000 ♦ · Oct 15, 2013 at 08:26 PM 0
Share

Is PlayerHealth a script or a variable? The error calls it a 'type'.

Paste the line that looks like

var PlayerHealth : int; or whatever it looks like in your code.

avatar image Chris498 · Oct 15, 2013 at 08:35 PM 0
Share

Wow, weirdly enough, there was no variable. Although, when I typed PlayerHealth, it showed it was there. I even used CTRL+F and could not find PlayerHealth anywhere else in the script.

avatar image meat5000 ♦ · Oct 15, 2013 at 08:43 PM 0
Share

So is there a script called PlayerHealth itself?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by meat5000 · Oct 15, 2013 at 09:24 PM

Ok, what the error is saying is that you are trying to compare a Type to an int. A type is 'int' itself not the value of int. A script is also considered a type as you can declare a variable of type 'script'

 var scriptVariable : Script; // variable scriptVariable of type Script, where 'Script' is the Actual name of a script
 var myNumber : int = 0; // variable myNumber of type int equal to 0

In your case you have a script called PlayerHealth and so your engine recognises it as a type so throws your error when you use it in your if statement comparison.

What you need to do is declare an integer and use that for your players health value.

 var playerHP : int = 100;
 
 function gameover()
 {
     if ( playerHP <= 20 )
     Application.LoadLevel("Gameoverscreen");
 }

In the if statement line

 if ( playerHP <= 20 )

playerHP is an integer so the if can make the comparison.

Understand though, I'm simply dealing with the error and not telling you how best to do whatever it is you are trying to do.

Comment
Add comment · Show 11 · 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 Chris498 · Oct 15, 2013 at 09:40 PM 0
Share

I am not sure what I did wrong here. I added the lines the the player's script. And it doesn't work. Even when I change var playerHP in lets see 110. It doesn't show up in the screen. I have the PlayerHealth script, but it isn't even attached to my Player, so how can that work out, since nowhere else in my player script there isn't even a mention about some ''PlayerHealth''. It is so weird I can't figure this out :(

avatar image meat5000 ♦ · Oct 15, 2013 at 09:47 PM 0
Share

As I say, I'm not telling what to do directly, but trying to make you understand the error.

There is probably a variable inside the PlayerHealth script that is used for the actual health value. Take a look through and see if you can find it.

Scripts are compiled and can be referenced even if not attached to an object.

Avoid using variable names that match the name of scripts.

avatar image Chris498 · Oct 15, 2013 at 10:12 PM 0
Share

I have found the variable, and I changed it with: var playerHP : int = 100; But when I change it to 120, in game it still shows 100. I have also tried adding the script to my player, that also doesn't seems to work. btw, thanks for helping me to understand it, it works a lot better then to just tell me the answer, thanks :)

avatar image meat5000 ♦ · Oct 15, 2013 at 10:17 PM 0
Share

Post the script

And yes, I agree :)

avatar image Chris498 · Oct 15, 2013 at 10:26 PM 0
Share

This is the PlayerHealth script, if you want me to post the player script, just tell me :)

 //------------------------------//
 //  PlayerHealth.js             //
 //  Written by Alucard $$anonymous$$      //
 //  5/10/2013                   //
 //------------------------------//
 
 #pragma strict
 
 var playerHP : int = 110;
 var healthDecayRate : float = 5.0;
 private var healthDecayRate$$anonymous$$odifier : float = 5.0;
 
 private var startingHealth : float;
 private var decay$$anonymous$$odifier : float;
 
 var staticRenderer : Renderer;
 
 
 function Start() 
 {
     startingHealth = playerHP;
     
     decay$$anonymous$$odifier = startingHealth / healthDecayRate;
     
     healthDecayRate$$anonymous$$odifier = ( healthDecayRate - 0.6 ) / 7.0;
     
     if ( !staticRenderer )
     {
         Debug.LogWarning( "No Static Renderer Object in the Inspector" );
         
         var staticObject : GameObject = GameObject.Find( "StaticObject" );
         
         if ( staticObject )
         {
             staticRenderer = staticObject.renderer;
         }
         else
         {
             Debug.LogWarning( "No StaticObject found ...." );
         }
     }
     
     staticRenderer.material.color.a = 0.0;
 }
 
 
 function DecreaseHealth() 
 {
     playerHP -= decay$$anonymous$$odifier * Time.deltaTime;
     
     // calculate the alpha
     var newAlpha : float = 1.0 - (playerHP / startingHealth);
     
     staticRenderer.material.color.a = newAlpha;
     
     // check if playerHP is below 0
     if ( playerHP <= 0.0 )
     {
         playerHP = 0.0;
         
         Debug.Log( "Player is OUT OF HEALTH" );
         
         
         
         // lose condition
         
         // load game over scene
         Application.LoadLevel( "Gameoverscreen" );
     }
     
     Offset$$anonymous$$ainTexture();
 }
 
 
 function IncreaseHealth() 
 {
     playerHP += decay$$anonymous$$odifier * Time.deltaTime;
     
     // calculate the alpha
     var newAlpha : float = 1.0 - (playerHP / startingHealth);
     
     staticRenderer.material.color.a = newAlpha;
     
     // check if health is above startingHealth
     if ( playerHP >= startingHealth )
     {
         playerHP = startingHealth;
     }
     
     Offset$$anonymous$$ainTexture();
 }
 
 
 function DecreaseHealthDecayRate()
 {
     healthDecayRate -= healthDecayRate$$anonymous$$odifier;
     
     decay$$anonymous$$odifier = startingHealth / healthDecayRate;
 }
 
 
 function Offset$$anonymous$$ainTexture() 
 {
     var rndXoffset : float = Random.value;
     var rndYoffset : float = Random.value;
     
     staticRenderer.material.mainTextureOffset = Vector2( rndXoffset, rndYoffset );
 }
 
Show more comments

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

15 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Item Scripting Error 2 Answers

Serialization scripting error 1 Answer

Error with script. 1 Answer

Can't convert int to bool? 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