Question by
BluetheFox · Sep 10, 2016 at 08:42 AM ·
if statementno error
if statement being completely ignored c#
using UnityEngine;
using System.Collections;
public class Clickvalve : MonoBehaviour
{
// From the inspector, add the player GameObject on this script's field
public Transform Player;
// Exact same thing as player
public Transform Box;
// Set the desired interaction distance value
public float InteractionDistance = 10;
// Setting up the "playing" variable
bool playing;
// Setting up a public variable to be referenced by another script later
public static int Valvepressure = 10;
//following section updates every single frame
void Update()
{
//if the animation is playing...
if (GetComponent<Animation>().IsPlaying("Valve_turnright"))
{ //...then set "playing" to true
playing = true;
}
else //otherwise...
{ //...set it to false.
playing = false;
}
}
//now, when I press the mouse button...
void OnMouseDown()
{ //Playerdist is defined as the distance between player and box
float Playerdist = Vector3.Distance(Box.position, Player.position);
//then if the player distance is less than the interaction distance and the animation isnt playing...
if (Playerdist < InteractionDistance & !playing);
{// it should do all this. and it does...but for some reason, it skips the if statement.
GetComponent<Animation>().Play("Valve_turnright");
GetComponent<AudioSource>().Play();
if (Valvepressure > -6)
{
Valvepressure = Valvepressure - 1;
}
Debug.Log("Valve pressure is now" + Valvepressure + "!");
}
}
}
Comment
Best Answer
Answer by unityGecko · Sep 10, 2016 at 10:45 AM
You have a semicolon in line 37, right after the if-condition. Thus, the C# compiler thinks your if-statement has an empty body and moves on to the next line.
Just delete the semicolon and it should run just fine.
Your answer
Follow this Question
Related Questions
Zombie Attack Animation script 1 Answer
no idea what is wrong 0 Answers
how to reset jump everytime it hits ground not when it is constantly on the ground 0 Answers
If & Else If with == and && not working 0 Answers
Unexpected token: if HELP 1 Answer