- Home /
SetActive (true); Used the wrong way?
So This is some easy code, depending on what button I press on my Canvas and each button represents different cars, like Audi, BMW and Special car! The value changes depending on the different buttons and the car models changes depending on the value
Button --[changes]--> Value --[changes]--> Car
Now I was testing the " GameObject.SetActive(true/false) " now I got a big issue the object does not change. Now the Value changes and the msg is sent out in the log. But the Game Object or the Car, in this case, doesn't change. But the Value does, the msg get printed. Which mean either two things > A: the engine is mishandling the request to make "cars" appear and disappear > or > B: There is another unknown script within my game that I have created that overwrites the decision. The thing is I checked and the only other script in the game is a level changer that reacts to another UI object, think it's related? Because I can't draw a correlation between them.
Now I build everything like it's done in this guide BTW: https://www.youtube.com/watch?v=kTDlCK4g-8A
using UnityEngine; using System.Collections;
public class CarSelector : MonoBehaviour {
public GameObject BMW;
public GameObject AUD;
public GameObject SPE;
public int carSelected;
void Start () {
BMW.SetActive (true);
AUD.SetActive (false);
SPE.SetActive (false);
carSelected = 1;
}
public void loadBMW () {
BMW.SetActive(true);
AUD.SetActive(false);
SPE.SetActive(false);
carSelected = 1;
Debug.Log("car 1 was selected, BMW m4 n34");
}
public void loadAUD () {
BMW.SetActive(false);
AUD.SetActive(true);
SPE.SetActive(false);
carSelected = 2;
Debug.Log("car 2 was selected, AUDI");
}
public void loadSPE () {
BMW.SetActive(false);
AUD.SetActive(false);
SPE.SetActive(true);
carSelected = 3;
Debug.Log("car 3 was selected, Ferrari 250gt");
}
}
Here is my level changer, the weird part is that is that it work when changing scenes but can't disable/false objects on UI level
using UnityEngine;
using System.Collections;
public class LevelLoader : MonoBehaviour
{
public GameObject Menu_Canvas;
public GameObject settings_Canvas;
void Start()
{
Menu_Canvas = GameObject.Find("MainMenu_Canvas");
settings_Canvas = GameObject.Find("Settings_Canvas");
settings_Canvas.SetActive(false);
Menu_Canvas.SetActive(true);
}
public void LoadLevel(int a)
{
Application.LoadLevel(a);
}
public void Quit()
{
Application.Quit();
}
public void loadMenu()
{
Menu_Canvas.SetActive(true);
settings_Canvas.SetActive(false);
}
public void loadSettings()
{
Menu_Canvas.SetActive(false);
settings_Canvas.SetActive(true);
}
}
Thx for all the Answers - Love to improve this question // if it's answer destroy this post and send me there!
I'm pretty sure that's not a bug in Unity, there must be something else going on.
From the code you've shown, one explanation could be that Start() gets called again for some reason. Put another Debug.Log in there to check.
Do I understand that your GameObjects are not in the same scene as the UI? How do you link them then?
$$anonymous$$ake sure the car variables don't point to the same object and that there are no errors in the console.
not very helpful, as I stated earlier I already check the variables and the syntax
Answer by Geometrical · Dec 23, 2016 at 12:54 AM
Make sure that the car objects reference your cars in the scene (pre-instantiated game objects) and not the prefabs in the Unity Asset Database (the project's asset folder).
If that isn't the case then make sure the script in your editor matches the code-inspector in Unity of your script. Other than that the code seems fine and should do what it's intended to do.
I can't seem to find a solution so I am abandoning this project for now thanks anyway
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Request feedback for C# script (instantiating UI elements depending on Player Input) 0 Answers
UI Decreasing Timer Bar Not Executing Print? 1 Answer
UI text showing money 2 Answers