Hello
How do you make a script for this.
When Player triggers the box collider Print “you win”
When Enemy triggers the box collider Print “you lose”
Thanks
Look up OnTriggerEnter(), and look into ‘tags’ for determining whether it was the player or an enemy that entered the trigger.
For printing, it depends on what you mean. If you just want to display some debug output to see if it’s working, you can use Debug.Log(). If you want it to be an in-game message, there are multiple ways you could do it, but an obvious solution would be to use OnGUI() and (e.g.) the GUI ‘label’ control.
Hello
It needs to be a text that pops up in big letters in the middle of the screen saying, YOU WIN or YOU LOSE.
I tried to use OnGUI and the trigger, but the problem is that I dont know how to put the trigger with the GUI.
The farthest I got was that it printed you win, or you lose in the debug thing.
Thanks
As far as printing the results using Debug.Log() goes, did everything work correctly? That is, were the appropriate messages printed at the appropriate times?
If so, it sounds like the only task remaining is to get that message on screen rather than only in the console. There are many ways this can be done, and I’m not going to try to cover them all here, but I’ll just offer one example.
One way to do it would be to add the OnGUI() function to the same script that has the OnTriggerEnter() function. Then, create a variable or set of variables that indicates whether the game is still in progress, whether the player has won, or whether the player has lost (an enum might be a good choice for this). Initialize the variable(s) to ‘game in progress’. If the player enters the trigger, change this to ‘player won’, and if the enemy enters the trigger, change this to ‘player lost’. Then, in the OnGUI() function, display the ‘you won’ message or the ‘you lost’ message or do nothing at all depending on the state of the aforementioned variable(s).
Thanks
But could you show it becasue I tried many times and I am new to scripting.
Thanks a lot
If you’ve tried many times, perhaps you could post one of your attempts along with any error messages you got and/or a description of how it’s not working.
The code in its simplest form would be pretty simple, and you might get someone to post a complete example for you. But, IMO, there’s more to be gained from trying to break the problem down and solve it logically yourself.
In my earlier post I offered a step-by-step description of how it might be done. The first step was to create a variable or set of variables that indicates the current state of the game; that is, whether the game is in progress, or whether the player has won or lost (three states). So, I would start there. If you’re not sure how to do that part, post your attempt along with any specific questions you have, and I or someone else will be able to help you sort out the problems and move on to the next step.
Hello
This is my script.
function OnTriggerEnter(other:Collider) {
if (tag != “Player”)
print (“You Win!”);
if (other.tag != “Player”)
print (“You lose!”);
}
And the errors I get are in the pic.
Also if I want to post my game on a webplayer
How do I do that
Thanks
Well, those aren’t errors, per se - that’s just the output of your program
What game object is the script attached to? Is it attached to the player? The enemy? The ‘trigger’ object?
[Edit: Since creating a webplayer for your game is an unrelated topic, I’d recommend creating a different thread for that.]
hello
I know those are errors.
I just wanted to know why it did it 10 times and saying you win and you lose.
The script is attacted to the finish line, the trigger object.
I will post a tread on how to create a webplayer ok.
No, I said they aren’t errors, not they ‘are’ errors.
Ok, so if the script is attached to the trigger object, here’s a couple of things to consider. First of all, you’re checking the value of ‘tag’ in one of your conditionals, and the value of ‘other.tag’ in the other conditional. ‘tag’ is the tag that’s associated with the trigger object, and ‘other.tag’ is the tag of the object that’s entered the trigger. The question to ask is, which tag are you really interested in? Are you interested in the tag of the object that’s entered the trigger? Or of the trigger object itself? The answer to that question should give you a hint as to how your logic needs to be restructured.
Also, be sure you’re using the appropriate comparison operators (== means ‘equals’, and != means ‘unequals’).