- Home /
How to call a button to specific levels without using the OR (||) function?
Hi everyone. I am having a little trouble making a GUI button clickable on specific levels.
My script is included for you to see what I am trying to accomplish:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WinButton : MonoBehaviour {
public int currentlevel;
public AudioClip btnClickedSound; //audioclip for buttons
public Sprite btnActive; //sprite representing active state of button
private SpriteRenderer spriteRenderer;
private Sprite initialSprite;
// Use this for initialization
void Start () {
initialSprite = this.GetComponent<SpriteRenderer>().sprite; //save object's sprite to an initial variable
spriteRenderer = GetComponent<Renderer>() as SpriteRenderer;
}
/// Raises the mouse down event.
void OnMouseDown()
{
spriteRenderer.sprite = btnActive;
PlayAudioButtons();
}
/// Raises the mouse up event.
void OnMouseUp()
{
spriteRenderer.sprite = initialSprite; //reset the button sprite to its initial normal state
if (currentlevel == 15 || 30 || 45 || 60 || 75 || 90 || 105 || 120 || 135 || 150 || 165 || 180 || 195 || 210 || 225 || 240 || 255 || 270 || 285 || 300 || 315 || 330 || 345 || 360 || 375 ||
390 || 405 || 420 || 435 || 450 || 465 || 480 || 495 || 510 || 525 || 540 || 555 || 570 || 585 || 600 || 615 || 630 || 645 || 660 || 675 || 690 || 705 || 720 || 735 || 750) {
UIAssistant.main.ShowPage ("CardPopup");
} else if (currentlevel != 15 || 30 || 45 || 60 || 75 || 90 || 105 || 120 || 135 || 150 || 165 || 180 || 195 || 210 || 225 || 240 || 255 || 270 || 285 || 300 || 315 || 330 || 345 || 360 || 375 ||
390 || 405 || 420 || 435 || 450 || 465 || 480 || 495 || 510 || 525 || 540 || 555 || 570 || 585 || 600 || 615 || 630 || 645 || 660 || 675 || 690 || 705 || 720 || 735 || 750) {
SessionAssistant.PlayNextLevel;
}
}
void PlayAudioButtons()
{
GetComponent<AudioSource>().PlayOneShot(btnClickedSound, 0.5f);
}
}
Unity is displaying a message of:
error CS0019: Operator ||' cannot be applied to operands of type
bool' and `int'
How would I write the code to include 50 different levels that are clickable from one button?
Comment
Best Answer
Answer by tormentoarmagedoom · Apr 06, 2018 at 10:20 PM
Good day.
I see you need to know if the level is divisible by 15 right?
Then why dont create a for to check it? like:
public void OnMouseUp()
{
for (int i = 1; i*15 <= 750; i++)
{
if (i*15 == currentlevel)
{
One thing
return;
}
}
The Other thing
}
If helped, accept/upvote :D
BYE!