I have a question: I have a landscape and I would like to make a day, night and rain scene, and I would change the objects in the landscape. So for example I have a day scene with trees and NO bushes, and a day scene with trees AND bushes, and a day scene with trees, no bushes and a house. (the same for the night and rain scene) How can I switch between that? I can make a whole new landscape (than I have many landscapes) but can I also make some buttons? (Like: choose your landscape / add bushes / add house or not?) How can I do that? The solution (like the buttons) must be ingame, not in the scene where you can edit. So for example, if you go ingame you can see a button and if you click on the button, the trees will appear or dissapear (and the same for the house / bushes /etc. / etc.)
I’ll go ahead and provide a link to your Unity Answers thread, just so people don’t end up duplicating their efforts. (You might consider editing your UA post and linking back to this thread as well.)
For the interface, you can use GUI/GUILayout controls such as buttons, toggles, etc. (whichever is appropriate for what you’re wanting to do). You can find an introduction to using the GUI system here.
To show/hide all trees/bushes, etc., you can find all the corresponding objects by name or tag (or type, technically, although you’d then need a ‘tag’ type of some sort associated with each object), and then enable/disable the renderers for those objects as appropriate (or just activate/deactivate the object itself). You could also group like objects together under a single parent object, and then simply activate/deactivate that object recursively using GameObject.SetActiveRecursively().
If you’re still not sure how to proceed, perhaps you could specify which part you need help with.
Can you clarify your question? The link I gave you (the ‘GUI Scripting Guide’) shows you how to make buttons and other controls. Also, what are the empty objects you refer to?
So I made a empty gameobject and I attached a script to it. But it doesn’t work. Where should I paste the script, and to what gameobject should I attach it?
Ok, sorry, the buttons work now. But how can I appear and disappear objects? I have now in the hierarchy a object Button (the script with the buttons). I have also a map Extended with trees and bushes in it. How can I (disappear) objects in that map? My script:
function OnGUI () {
// Make the first button.
if (GUI.Button (Rect (20,40,80,20), "Level 1")) {
gameObject.SetActiveRecursively(false);
}else{
gameObject.SetActiveRecursively(true);
}
// Make the second button.
if (GUI.Button (Rect (20,70,80,20), "Level 2")) {
}
}
if (GUI.Button (Rect (20,40,80,20), "Level 1")) {
gameObject.SetActiveRecursively(!gameObject.active);
}
this will toggle it on each click. your code would only hide it while the button is pressed with which this type of button means a single frame and a lot of stutter
That’s probably because FindWithTag() ignores inactive objects, so once you’ve deactivated it, it doesn’t show up in the search anymore.
There are other ways you can solve the problem though. One solution (for example) would be to find all the objects in question by tag or by name (for example, you could tag all the trees in your scene as ‘Tree’ and then use FindGameObjectsWithTag() to acquire an array of references to these objects), and then enable/disable the renderers for those objects as appropriate. Or, you could keep your ‘group’ object and iterate over the children of that object, again activating/deactivating renderers as appropriate.
I have on more question. I hope you can help me. I am not good in scripting, so how can I change the scene? I mean, I have a sun, cloud, night and normal scene. In the sun scene I would like to place a sharp yellow light, in the cloud a grey light, in the night a black light and in the normal scene a normal yellow light. How can I switch between that with one button? It must start with a sun scene, and if you click on the button the sun light will be hidden, and the grey light will be visible, and then if you click on the button the grey light will be replaced by the black light, and after one click the black one will be replaced by a normal yellow light. After the next button click it will start from the beginning (the sharp yellow light). How can I do this?
Well, the easiest solution would probably be to make each of the settings you described (day, night, cloudy day, etc.) a different scene and then switch between them using Application.LoadLevel().
If you want to make these changes within the same scene however, then you’ll have to learn about lighting and other related effects and then enable/disable/modify these effects via scripting as needed.
At its simplest, the things you’d probably need to change would be:
The ambient light value for the scene
The values for a single directional light (which could be used to represent the sun for day scenes and the moon for night scenes)
The skybox material (different materials for day/night, cloudy/clear, etc.)
If I were you, I’d probably just start with one of these parameters and see if you can get it to change dynamically in response to GUI events (buttons, etc.). The ambient light value would probably be a good place to start, as it’s a simple parameter that’s easy and intuitive to modify (by simply changing the ambient light value, you can probably get a decent approximation of the lighting characteristics for a clear day, a cloudy day, and night, provided you don’t have any other strong light sources in the scene).
I have already made some lights for day/night/clouded etc. How can I switch between them like my post before this post? That is the script I need. I mean, it is not necessary to make different scenes, because I have already made the lights. As I said in my first post, I am going to make a printscreen from my landscape, so people dont see the skybox.
I would start here. I haven’t done any scripting of light sources myself, but I can only assume it works as expected - that is, that you can enable/disable and change the properties of lights at run time and see the effects immediately.
Assuming that’s true, then changing the lighting in your scene will be very similar to hiding/showing different objects in your scene as discussed previously; you just need to acquire references to the light components (using whatever method you prefer), and then change the properties as desired.
If that doesn’t give you enough to go on, perhaps you could specify exactly which part you’re not sure how to do.
Ok, I hope you understand me, but the problem is that I dont know how to switch between objects with one button. For example I have 4 objects:
Object 1
Object 2
Object 3
Object 4.
And I have one button,
Standard in the game mode the Object 1 is visible and objects 2,3 and 4 are hidden. If I click on the button, Object 1 will be hidden, Object 2 visible and Object 3 and 4 hidden. If I click on the button the objects 1 and 2 will be hidden, 3 visible and 4 hidden. If I click for the third time on the button, object 1,2 and 3 will be hidden, and object 4 visible. And if I click for the fourth time on the button, if will start from te beginning (Object 1 visible, and objects 2,3 and 4 hidden).
// This all goes in a class derived from MonoBehaviour:
public GameObject[] targets;
int currentlyVisible = 0;
void Start()
{
Enable(currentlyVisible);
}
void OnGUI()
{
// Using GUILayout for the sake of example, but you can of course
// use the GUI class instead.
if (GUILayout.Button("Switch")) {
currentlyVisible = (currentlyVisible + 1) % targets.Length;
Enable(currentlyVisible);
}
}
void Enable(int index)
{
// This code assumes that each target has a renderer that you
// want to enable/disable. If you want to enable/disable something
// else (e.g. a light), you'll need to adjust the code accordingly.
for (int i = 0; i < targets.Length; ++i) {
targets[i].renderer.enabled = (i == index);
}
}
I have now the next code in my javascriptfile buttonscript.js:
function OnGUI () {
if (GUI.Button (Rect (20,70,80,20), "Tijd")) {
var object1 = GameObject.Find ("Treeobject");
object1.SetActiveRecursively(!object1.active);
}
if (GUI.Button (Rect (20,40,80,20), "Level 1")) {
// This all goes in a class derived from MonoBehaviour:
public GameObject[] targets;
int currentlyVisible = 0;
void Start()
{
Enable(currentlyVisible);
}
void OnGUI()
{
// Using GUILayout for the sake of example, but you can of course
// use the GUI class instead.
if (GUILayout.Button("Switch")) {
currentlyVisible = (currentlyVisible + 1) % targets.Length;
Enable(currentlyVisible);
}
}
void Enable(int index)
{
// This code assumes that each target has a renderer that you
// want to enable/disable. If you want to enable/disable something
// else (e.g. a light), you'll need to adjust the code accordingly.
for (int i = 0; i < targets.Length; ++i) {
targets[i].renderer.enabled = (i == index);
}
}
}
}
It doesnt work. I get the next errors:
Assets/Buttonscript.js(10,41): BCE0043: Unexpected token: public.
Assets/Buttonscript.js(10,47): UCE0001: ‘;’ expected. Insert a semicolon at the end.
Assets/Buttonscript.js(10,58): UCE0001: ‘;’ expected. Insert a semicolon at the end.
Assets/Buttonscript.js(10,60): UCE0001: ‘;’ expected. Insert a semicolon at the end.
Assets/Buttonscript.js(12,44): UCE0001: ‘;’ expected. Insert a semicolon at the end.
Assets/Buttonscript.js(14,45): UCE0001: ‘;’ expected. Insert a semicolon at the end.
Assets/Buttonscript.js(14,53): UCE0001: ‘;’ expected. Insert a semicolon at the end.
Assets/Buttonscript.js(16,73): BCE0044: expecting :, found ‘;’.