Two 'If' requirments SOLVED

So, I get these three errors with this code. Any help?

Assets/Scripts/ToolsChanger.js(10,32): BCE0044: expecting ), found ‘=’.

Assets/Scripts/ToolsChanger.js(10,34): BCE0043: Unexpected token: 0.

Assets/Scripts/ToolsChanger.js(11,36): BCE0044: expecting :, found ‘=’.

var Tools : Tools; //This will become a link to your player tool GUI.
var ToolsCount = 0;

function Start () {
     Tools =  FindObjectOfType(GUI); //This caches a link to that script.
}

function Update  () {
	if (Input.GetKeyDown ("q")) {
		if (ToolsCount = 0) {
			ToolsCount = 1;
		}
	}
	if (Input.GetKeyDown ("q")) {
		if (ToolsCount = 1) {
			ToolsCount = 2;
		}
	}
	if (Input.GetKeyDown ("q")) {
		if (ToolsCount = 2) {
		ToolsCount = 3;
		}
	}
	if (Input.GetKeyDown ("q")) {
		if (ToolsCount = 3) {
			ToolsCount = 1;
	}
}

I assume you’re new to coding? Welcome to the art of head-smash-to-desk! Haha. It’s not that bad :smile:

So, quick lesson.

If you have two conditions that must be met use “”

For example, let’s say you wanted to attack an enemy, and the two constraints were that you were close to him, and you pressed a button:

if (CloseToEnemy() == true  ButtonWasPressed() == true) {
   ExecuteCode ();
}

Here, CloseToEnemy() would be a function that returns a boolean, and same with ButtonWasPressed.

A shortcut to writing that would be :

if (CloseToEnemy()  ButtonWasPressed()) {
   ExecuteCode ();
}

Here it’s the doing the same thing. For example, if you have a boolean :

var didEat : boolean;

It would default didEat to false. If we changed it, so that it was true:

 didEat = true;

Then we can refer to the expression as :

 if (didEat) { 
   RunCode();
}

Also if we wanted to do something when we didn’t eat,

 if (!didEat) { 
   RunCode();
}

You might have known some of that, but I’m assuming you didn’t considering you didn’t know the “”, which represents an AND condition. Compared to a “||” which represents an OR condition.

Using what I just said should help you understand how to fix your problem (use the instead of two different if statements.)

In addition, I’d highly suggest you go out and buy a programming book to learn all the basics of programming!

Have fun!

What gamesurgeon said is true but that’s not why your script crash.

if (ToolsCount == 0) { 
     ToolsCount = 1; 
}

On the first line, we want to test if ToolsCount is equal to zero so we use 2 equal signs.
On the second line, we want to set the value of ToolsCount to 1 so we use only 1 equal sign.

So first, try to fix your equal signs and then, read again gamesurgeon’s advices. You will then be able to wrap 2 if statements in only one.

Good luck :wink:

As Aegis said, you need to use two == for comparison to check if something is equal to another or != to check if it is not equal to.

What you are doing is just assigning values instead of comparing them.

Hey, thanks all you guys for your great support! It all worked and I learned a lot. I can’t thank you enough!

As third step, you could watch over if, elseif else not only if :wink:

If I want something with 3 conditions, can it be done like this?

if (Input.GetKeyDown(KeyCode.M)  miniBigCam.enabled == false  miniCam.enabled == false) {

}

Cause mine don’t seem to be working. D:

Yup. You can even use parentheses to group conditions together:

if ( (Input.GetKeyDown(KeyCode.M)  miniBigCam.enabled == false) || (miniCam.enabled == false) )
{
}

|| is “OR” by the way.