- Home /
What type of sign do I use for a Scene?
I was trying to make this script reusable. I did not want to make the same script three million times that does the same thing. So, I wrote this simple script and I used a string. The problem with that was when I wanted to practically apply the script, the scene did not load. I understood this was because of the sign, but I do not know what to put instead of a string in order to make this work. So please help me. I'm sure it's not to hard. The script will be placed at the bottom.
using UnityEngine;
using System.Collections;
public class TransferLevels : MonoBehaviour {
public string toLevel;
void OnMouseDown () {
Application.LoadLevel( toLevel );
}
}
you can set the string in the inspector, it is public. so you can attach the script to any gameobject and have different strings.
Answer by Slobdell · Jun 30, 2013 at 06:58 PM
toLevel has no value, so your loading null. You forgot to put the level name in the string.
That's the problem. I don't want to have to make multiple of this same script and have to change the string in each. I want to make it so I can attach this script and the drag a level to it in the inspector.
I don't understand what you're talking about. You can use the same script you just have to say toLevel = "theNameOfYourLevel"
I don't want to have to do that. I don't want to have to copy the script and change the string each time I reuse it.
Answer by Bunny83 · Jul 01, 2013 at 11:02 PM
I don't see any problems with this script. As long as you added all your scenes to the build menu (like @quest-23 said) you should be able to load the level via it's name.
Have you actually started to debug your problem? For example by putting this line before your LoadLevel call:
Debug.Log("TransferLevels:OnMouseDown:LoadLevel:" + toLevel );
When you click on the collider of the object your script is attached to, do you get this log?
You are correct there is not problem with it. What I'm saying is I want to change it so I don't have to copy the script and go in and change the string in order to use it for the same thing, just different scene. I want it so it's one script that I can attach to all the objects and then drag and drop the scene into the inspector ins$$anonymous$$d of having to go in and change the string.
There's a communication breakdown somewhere here. What you're saying doesn't make sense. You need to explain better what it is you're trying to do. Why are you dragging and dropping scenes in the inspector, if that's even possible? You have to load scenes...
There is not scripting type for scene assets, you have to change the string in the inspector.
If you don't want to type that much, ins$$anonymous$$d of a string you can use an integer and use it as level index. $$anonymous$$eep in $$anonymous$$d that when you change the level order in the build settings the index of the following levels will change.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Changing Mesh of an object depending on Health Value. C# 2 Answers
Pushing More GUI Clips Than Popping 0 Answers
Trouble using C# tooltips 1 Answer