- Home /
2 conditions for a door to open
I have an animated door with a trigger collider so that OnTriggerEnter ( the player) the door opens. Piece of cake no problem. I would, however, like to make the door open only if another condition exists and that being the player has more than 9 points. ( my point system is in 2's so instead of saying => than 10 I just say >9 which amounts to the same thing since getting 9 points is impossible). I have gone through the beginners tutorials and the "coffee too hot or cold" example doesn't seem to work - at least not how I implimented it. When I score points in the first place the code reads "ScoreManager.score += scoreValue;" so I must need to access that somehow but I cannot figure it out. Below is the script I am using that works for the door to open with just the single variable of the player entering the triggerzone. All the commented out stuff were my attempts to add the other condition of having >9 points which only broke the script. Please help and please mr/miss moderator do not reject my question. i feel it is a valid topic that others may be wondering the same. I find having a point system quite useless unless it is a means to something greater - like opening a door to another gameplay area . then I guess the cost of the door opening needs to be subtracted from player point total but that is the least of my concerns and I can probably figure that out by reversing the adding score parts from the scoring script. So here is my butchered script with the fails commented out.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class NewOpen : MonoBehaviour {
//public Score score; // your score variable
//private GameObject player;
//private ScoreManager;
public AudioClip doorOpenSound;
//public int scoreValue > 09;
//private ScoreManager scoreManager;
//void Awake ()
//{
//scoreManager = ScoreManager.score;
//}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player");
//{
//if (scoreManager > 09);
animation.Play();
AudioSource.PlayClipAtPoint(doorOpenSound, transform.position);
}
}
Some things don't add up here, as far as I am concerned :)
//private Score$$anonymous$$anager; You define a variable "Score$$anonymous$$anager" but don't give it any variable name. Ok so far since it's commented out, but further down below:
//private Score$$anonymous$$anager score$$anonymous$$anager;
is another variable of type Score$$anonymous$$anager, this time properly declared with a name. So I guess this is the right one, and you can and should immediately delete the first one.
The line //public int scoreValue > 09; uses wrong syntax. To declare a variable in the main body, you declare it's access type (public/static/private), it's type, it's name, and it's value (if you want). In your line, you to the first three steps right, but the "> 09" is completely wrong. Declare it this way:
public int scoreValue = 09;
This line:
//score$$anonymous$$anager = Score$$anonymous$$anager.score;
I have no idea what it tries to do. If score$$anonymous$$ananger is of type Score$$anonymous$$anager, why should it be assigned a value "score"? Isn't score of Type Score, rather than Score$$anonymous$$anager? Also, the variable Score$$anonymous$$ananger doesn't exist (or, more to the fact, it isn't properly declared, as detailed above).
//if (score$$anonymous$$anager > 09);
Wait, why "score$$anonymous$$ananger"? score$$anonymous$$anager is of type Score$$anonymous$$anager, and not of type int! I assume you want to use scoreValue in the comparison, right? Like this:
if (scoreValue > 09);
As you can see, there a a lot of things seriously wrong with your code. I suggest looking at how to declare variables in C#, understanding the difference between variable name and type, and then taking my obversations and corrections and trying it again :)
well it goes without saying I am not a programmer and am learning on the fly. I do have a question about things that are commented out. I get that the commands are ignored but does having them in there slow down the performance? I have since reworked the script and it does the job. I know I shouldn't have the 3 if statements under the OnTriggerEnter function
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class NewOpen : $$anonymous$$onoBehaviour {
public int doorFee;
public AudioClip doorOpenSound;
private GameObject HUDCanvas;
private GameObject DoorCollider;
public AudioClip[] DeniedClips;
public Animation anim;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
if (Score$$anonymous$$anager.score > doorFee)
animation.Play();
Score$$anonymous$$anager.score -= doorFee;
AudioSource.PlayClipAtPoint(doorOpenSound, transform.position);
{
if (other.tag == "Player")
if (Score$$anonymous$$anager.score < doorFee)
audio.clip = DeniedClips[Random.Range(0, DeniedClips.Length)];
audio.Play();
}
}
}
actually I combined the 3 if statements in the code I just pasted