var isQuitButton = false;
var LoadLevel = 2; //Show in inspector
function OnMouseEnter()
{
//change the color of the text
renderer.material.color = Color.red;
}
function OnMouseExit()
{
//change the color of the text
renderer.material.color = Color.white;
}
function OnMouseUp()
{
//are we dealing with a Quit Button?
if( isQuitButton )
{
//quit the game
Application.Quit();
}
else
{
//Load Level
Application.LoadLevel(1);
}
{
//Load Level
var LoadLevel = 2;
}
}
Well that is my script, I got the main form from TornadoTwins and that has helped me in my main menu but I don’t want to create multiple scripts just based on one so I want to put all my go to the next level scripts in this one. At the bottom and top I added some new things that are not working if somebody can tell me what is wrong please do.
var LoadLevel = 2; //Show in inspector
...
{
//Load Level
var LoadLevel = 2;
}
For starters, while you assign LoadLevel=2 in a couple places, you never actually use the variable. You are probably setting the variable because you want it to be used, so the next step is to use it. 
Also, since you initialize LoadLevel to 2 at the top of your script and you don’t modify it anywhere else, it’s kinda redundant to set it to 2 again at the bottom. (The only exception would be in the unlikely case that you have another script that accessed this one and changed LoadLevel from time to time.)
You probably want something looking like this
var isQuitButton = false;
var LoadLevel = 2; //Show in inspector
function OnMouseEnter()
{
//change the color of the text
renderer.material.color = Color.red;
}
function OnMouseExit()
{
//change the color of the text
renderer.material.color = Color.white;
}
function OnMouseUp()
{
//are we dealing with a Quit Button?
if( isQuitButton )
{
//quit the game
Application.Quit();
}
else
{
//Load Level
Application.LoadLevel(LoadLevel);
}
}
That transports me to level 2 each time. For the main part of the script I want it to be the TornadoTwins main menu script but at the bottom I want my own part of the script attached. Because in there I want if I attach the script to anything I want a bounding box to appear in the side corner and ask if I want to go to the next level. That is what I want the bottom part to happen. If it is possible for that to happen please post it on.
The variable LoadLevel can be seen in the inspector. For each object that the script is attached to, click on the object and look in the Inspector at the script. There will be a value called “LoadLevel”. You can change that value to load whichever level you want. The value in the inspector will be used instead of the default value (2) when you run the scene.
I know that, but that isn’t what is happening. Here is a simplified version of what I want. I am using the script for two things. For the main menu and for when I enter a new level. So the LoadLevel part, that part I want only to use for going to new levels. So I want one part for the main menu and one part for loading the new level. So if somebody can put the part load level 2 in a part like function late() and have it in there for the new level to load that is kind of what I want.
More basic terms: I want for one part to work with my main menu, because what I have right now keeps sending me to level 2. And I want the other part to work with going to new levels like I trigger something and I go to the next level.
var isQuitButton = false;
var LoadLevel = 2; //Show in inspector
function OnMouseEnter()
{
//change the color of the text
renderer.material.color = Color.red;
}
This is what I meant but I get an error when I try it like this as well. Can somebody fix this.
function OnMouseExit()
{
//change the color of the text
renderer.material.color = Color.white;
}
function OnMouseUp()
{
//are we dealing with a Quit Button?
if( isQuitButton )
{
//quit the game
Application.Quit();
}
}
function LateUpdate()
{
{
//Load Level
Application.LoadLevel(LoadLevel);
}
}
So you want a script that can be attached to objects so that when object is Selected/Triggered etc it will load the appropriate level.
This would depended on wether these are activated by Colliding, Entering a Area (trigger) or by selecting object with a mouse.
These are all seperate functions.
Assuming your where using a Collider marked as a trigger the following would work
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
//Display in Editor
public int loadLevel = 0 ;
void OnTriggerEnter() {
//Load the requested level
Application.LoadLevel(loadLevel) ;
}
}
You would attach this to your trigger, and set the target level in the editor.
Note you would probably need to make sure your player isnt Destroyed on Load, and will probably need to move the player to the appropriate area of the new level, else they will appear on the new level at the same coordinates as they left the last.
This will cause it to happen automatically. The LateUpdate runs at the end of the frame calculations. So this would get triggered every frame.
Where would I put this. Can you use my script and put where I would put it in there. So that way I can just copy and paste it. Because I get what you mean but I don’t get where.
I’ll explain my thoughts in more detail. I don’t know if I put that at the top of my script replacing the var I already have there. Or put it in the LateUpdate or replace the LateUpdate.
I was suggesting create a second script, rather than placing it in the existing.
However puting in your script
var isQuitButton = false;
var LoadLevel = 2; //Show in inspector
function OnMouseEnter()
{
//change the color of the text
renderer.material.color = Color.red;
}
This is what I meant but I get an error when I try it like this as well. Can somebody fix this.
function OnMouseExit()
{
//change the color of the text
renderer.material.color = Color.white;
}
function OnMouseUp()
{
//are we dealing with a Quit Button?
if( isQuitButton )
{
//quit the game
Application.Quit();
}
}
//function LateUpdate()
//{
// {
// //Load Level
// Application.LoadLevel(LoadLevel);
// }
//}
function OnTriggerEnter()
{
Application.LoadLevel(LoadLevel) ;
}
Apologies – I wrote the last script in C# not JS.
So what would it look like in two scripts, because that is the better thing to do because my game is an FPS Shooter and a big red thing keeps blurring my screen. And a lot of errors keep coming.
Just take the OnTriggerEnter function and the variable LoadLevel declartion and put it into a new javascript file.
Then attached to the trigger object. The OnTriggerEnter requires a collider that has been marked as a trigger.
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
//Display in Editor
public int loadLevel = 0 ;
void OnTriggerEnter() {
//Load the requested level
Application.LoadLevel(loadLevel) ;
}
}
function LateUpdate()
{
{
//Load Level
Application.LoadLevel(LoadLevel);
}
}
So like this.
You have multiple languages there
var loadLevel = 0 ;
function OnTriggerEnter() {
//Load the requested level
Application.LoadLevel(loadLevel) ;
}
You dont want the LateUpdate as that runs every physic update period (0.33 ms), just at the end of all the calculations. This would cause a level to try to load every period.
Thats it…
So if I attached this to a GameObject in my game and clicked on it I would go to the next level? Or would I need to do things to the script or to the GameObject first. Sorry if I’m bothering you I’m not that good with complicated scripts.