- Home /
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
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.
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.
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.
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.
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 :(
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.
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 :)
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 );
}
Your answer
Follow this Question
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