How do i detect if a certain cube is being touched?

I have a number of cubes on the screen and i want to touch one to change scene. All the cubes are flat and do not rotate.

So I’m asking how to make the condition to detect if the cube is being touched and then im going to put this line in the function Application.loadLevel(i);

Not sure if this is exactly what you are doing but I had a menu I created with 4 cube objects. I attached a script on each of them that where like that:

using UnityEngine;
using System.Collections;

public class startGame : MonoBehaviour
{   
    public string level;

    void OnMouseDown()
    {
        Application.loadLevel(level);
    }
}

As long as the mouse pointer is hovering the object, clicking calls OnMouseDown().

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseDown.html

I had the same problem a month ago! I ended up using OnMouseExit and OnMouseEnter found in the docs.

You can use Camera.ScreenPointToRay() and Physics.Raycast() to do a raycast from the point you are touching on screen. If that ray collides with one of your cubes you can have it load the appropriate scene.

for touching you could use OnTriggerEnter() Function.

So your code could look like this:

Function OnTriggerEnter()
{
function Application.loadLevel(i);
}

There are many answers so far, and most of the look useful. I enjoy writing scripts and this one took a bit to make, but it will work for any cube you use. You just drag it onto the cube you want and in the inspector, under this code’s title there will be a place where you need to input the name of the scene that you want it to load or the number of the scene based on its position in the build settings (i.e Application.LoadLevel(0):wink: will load the level with index 0. Heres the script:

// You can fill in both. It will by default use the levelNumber for the scene. If you want a cube to go to the third level you have just put in 2 for the levelNumber and leave the levelName blank. If you want to use a level name you just put in the name of the scene that you want to load.

var levelName: string;
var levelNumber: int = 0;

// this is the player or thing that will touch the cube. NOTE: in this script the player has the tag “Player”. To add tags just go to the top of the inspector and add a new tag if there isn’t already one called “Player”. Alternatively, you can call the tag something else, but you need to change this script. ONE MORE THING: if you’re player isn’t going to be in the game until later on, you need to change the start function to Update or do something because, the start function will go off as soon as the cube is in play.

private var player1: GameObject;

function Start(){
player =GameObject.FindWithTag(“Player”);
}

//Here is the function where you touch the cube. Since this is placed on the cube. Your cube will need a trigger, so that the player can touch it. To add a trigger go to: AddComponent => Physics => box collider. This will add you a trigger, which you can adjust in the inspector on the right. In the inspector make sure that the isTrigger option is ticked or this wont work.

function OnTriggerEnter(object: Collider){
	if (object == player){
		if (levelNumber){
		Application.LoadLevel(levelNumber);
		}
		if (levelName){
		Application.loadLevel(levelName);
		}
}
}

PS: One last important thing to note is that it is Application.LoadLevel and not Application.loadLevel and not function Application.loadLevel either

Try to use this function OnTriggerEnter.

Function OnTriggerEnter(col : Collider)
{
  if(col.name == "Cube")
  {
    Application.LoadLevel(1);
    Destroy(col.gameObject);
  } 
}

Attach this script to the cube and make sure that onTrigger is checked on your box collider.