I'm very new to scripting and here is my question: what is wrong with my script? no errors, I want to press a key (f) in my trigger to load a new level (my trigger is next to a door)
function OnTriggerEnter(){
if(Input.GetKey ("f"))
{
Application.LoadLevel("firsthouse");
}
}
Please read this carefully: http://docs.unity3d.com/ScriptReference/Input.Get$$anonymous$$ey.html
Why is this on OnTriggerEnter? OnTriggerEnter is for detecting collisions with gameObjects that have colliders attached to it. I would try putting this on Update, just as the example on the reference that @$$anonymous$$cGravity linked. http://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
Hi, first things first:
Please use a short, descriptive text for the questions title. Not the whole question.
This means logically, please describe your question in the questions text ;)
Please make sure your code is formatted properly. The first line is not formatted as code for example.
Here a re$$anonymous$$der: Frequently Asked Questions and the User Guide.
@itsharshdeep gave a accurate answer that solves what ever problem you might have. ;)
Your main problem as using OnTriggerEnter()
, which is only called once everytime something enters the trigger.
Answer by itsharshdeep · Nov 24, 2015 at 04:11 AM
Hello,
I don't know what you trying to achieve but as fas as I know there are two possibility you wanna achieve 1) When you will press "F" Buttons the scene should load.
void Update(){
if (Input.GetKeyDown (KeyCode.F)) {
Application.LoadLevel("SceneNameHere");
}
}
void OnTiggerEnter(){
// Do What you wanna do here :)
}
2) Now If you don't want the above thing but you need that when the OnTriggerEnter() should call then load the scene
void OnTiggerEnter(){
Application.LoadLevel("SceneNameHere");
}
*Note:- The only mistake in your code is, It will only execute only just when OnTriggerEnter() is called and the same time you press "F" button.
I'm also assuming that you want to do like, there is a region present in the scene and if the player is in that region & if s/he press the "F" button then the scene should load. If that is the case then you have to do the following :-
void OnTriggerStay(Collider coll){
if ( coll.transform.tag == "Player" && Input.GetKeyDown(KeyCode.F)){
Application.LoadLevel("SceneNameHere");
}
}
Answer by shane_123 · Nov 24, 2015 at 11:06 AM
This is in C# but Don't forget to go in to file, Build Settings... And add your levels
void Update () {
if(Input.GetKey(KeyCode.F))
{
Application.LoadLevel("firsthouse");
}
}
When posting answers containin code, please make sure your code contains no errors.
If(Input.Get$$anonymous$$ey($$anonymous$$eyCode.F))
throws a parsing error because keywords are case-sensitive.
Try to add the script to the player on your first level on your project. $$anonymous$$ake shure your using C#
I just found how to fix parsing error you need to find
public class NewBehaviourScript : $$anonymous$$onoBehaviour
You will need to change NewBehaviourScript to your script name
Again: if
needs to be lower case. Otherwise it will result in an error.
Yes the file name must match the class name. But that's not relevant here.
I suggest you have a look at the tutorials: IF Statements. ;)