Hey im trying to make it so when this Object is touched it moves to a Certain scene but instead of moving me to the scene The Jack Canyons Of Daniel it keeps moving me to The Unidentified Forest of Liquids would anyone happen to know where ive gone wrong in my code?
Here it is -
#pragma strict
import UnityEngine.SceneManagement;
var LoadScene: String;
function Update () {
if ( Input.GetMouseButtonDown(0))
{
if (Input.GetMouseButtonDown(0)) {
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
SceneManager.LoadScene("The Jack Canyons Of Daniel");
}
}
}
}
That is odd. Is it possible you have another script’s Update function checking for a LeftMouseClick and its loading the Forest of Liquids. THen its a race to see which one is called first.
Does any script/object in the Jack Canyon’s scene cause you go to Forest of Liquids. Maybe your actually loading jack canyons scene but something is then immediately loading the Forest of liquids.
Your code above is perfectly straightforward (other than the fact you check if the mouse button is clicked 2x in a row which seems unnecessary.) You should be loading the Jack Canyons Scene. Unfortunately your bug is some logical bug, not a wrong function call or syntax that is easy to track down.
From the Scene that has the Button to go to The Jack Canyons of Daniel there is also 2 other Areas one of which is The Forest Of Liquids which are attached to different objects so its 3 different sign graphics with 3 scripts attached that all move to different maps.
But no matter which object I click it goes to the Forest of Liquid - Is there something i would need to add to my code to make the objects differ?
Hard to say. Try naming all the signs something differernt (THeir GameObject names). and adding a debug log in the scripts near where you load the scene something like this:
if (Physics.Raycast(ray, hit)) {
Debug.Log(gameObject.name + " is loading scene Jack Canyon");
SceneManager.LoadScene("The Jack Canyons Of Daniel");
}
Do it for all 3 signs and see what the Debug shows you (obviously changing the Jack Canyon part to whatever scene that sign is suppose to load).
This script has a logical error. It basically says if the mouse is clicked, and a raycasts hits anything, then loads the next scene. Doing this will mean that the scene loads of you click on anything.
If you are doing it the same way on your button for the other scene, then that explains your problem.
You have two options.
Fix the logic error in your raycasting
Switch over to the EventSystem and let that do your raycasting for you.
@Kiwasi : Good Catch. Failed too notice he wasn’t checking what he hit.
BoredMormon has it correctly. You need to make sure your raycast is hitting the right sign. If all 3 of your scripts look like what you posted, All of them are just checking that they hit ANY sign… so all of them are triggering when you click on any sign. Its just random chance liquid forest gets loaded up first.
Its a noobie question but what would i have to write to make it check what object is being hit - the past day anything ive been trying hasnt been working to recognise what object it is attached to
if (Physics.Raycast(ray, hit)) {
if (hit.gameObject.name == "Jack Canyon Sign")
SceneManager.LoadScene("The Jack Canyons Of Daniel");
}
The above code would work if the sign your clicking on has its name in the Hierarchy called “Jack Canyon Sign”. You could also do the same thing with tags.