I’m working on an inventory system for my game, and I need help. I have this code which can change between 4 items, but for some reason, true and false don’t work with GameObjects (I heard it’s setActive or something). I don’t know, here’s my script, what do I do?
var radio : GameObject;
var lighter : GameObject;
var pipe : GameObject;
var none : GameObject;
function Start () {
none.enabled = true;
radio.enabled = false;
lighter.enabled = false;
pipe.enabled = false;
}
function Update () {
if (Input.GetButtonUp ("Fire2") && Input.GetAxis ("Vertical") > 0.5)
if(lighter.enabled == false){
lighter.enabled = true;
radio.enabled = false;
pipe.enabled = false;
none.enabled = false;
}
if (Input.GetButtonUp ("Fire2") && Input.GetAxis ("Vertical") < 0.5)
if(none.enabled == false){
none.enabled = true;
radio.enabled = false;
pipe.enabled = false;
lighter.enabled = false;
}
if (Input.GetButtonUp ("Fire2") && Input.GetAxis ("Horizontal") > 0.5)
if(pipe.enabled == false){
pipe.enabled = true;
radio.enabled = false;
lighter.enabled = false;
none.enabled = false;
}
if (Input.GetButtonUp ("Fire2") && Input.GetAxis ("Horizontal") < 0.5)
if(radio.enabled == false){
radio.enabled = true;
lighter.enabled = false;
pipe.enabled = false;
none.enabled = false;
}
}
Thanks!
Gilmar
February 8, 2016, 3:44am
2
You are totally correct in regards to using SetActive()
I got these errors. What did I do wrong?
var radio : GameObject;
var lighter : GameObject;
var pipe : GameObject;
var none : GameObject;
function Start () {
none.SetActive (true);
radio.SetActive (false);
lighter.SetActive (false);
pipe.SetActive (false);
}
function Update () {
if (Input.GetButtonUp ("Fire2") && Input.GetAxis ("Vertical") > 0.5)
if(lighter.SetActive (false)){
lighter.SetActive (true);
radio.SetActive (false);
pipe.SetActive (false);
none.SetActive (false);
}
if (Input.GetButtonUp ("Fire2") && Input.GetAxis ("Vertical") < 0.5)
if(none.SetActive (false)){
none.SetActive (true);
radio.SetActive (false);
pipe.SetActive (false);
lighter.SetActive (false);
}
if (Input.GetButtonUp ("Fire2") && Input.GetAxis ("Horizontal") > 0.5)
if(pipe.SetActive (false)){
pipe.SetActive (true);
radio.SetActive (false);
lighter.SetActive (false);
none.SetActive (false);
}
if (Input.GetButtonUp ("Fire2") && Input.GetAxis ("Horizontal") < 0.5)
if(radio.SetActive (false)){
radio.SetActive (true);
lighter.SetActive (false);
pipe.SetActive (false);
none.SetActive (false);
}
}
Error:
Assets/inventorySystem.js(22,35): BCE0026: ‘void’ cannot be used in a boolean context.
SetActive changes the “active” value you can’t use that to check if the gameobject is active (the setting function doesn’t have a return type so it returns “void”, hence the error, using a void and expecting a boolean)
you need to use “activeSelf” or “activeInHierarchy” to check if the gameobject is currently active
http://docs.unity3d.com/ScriptReference/GameObject.html
LeftyRighty:
SetActive changes the “active” value you can’t use that to check if the gameobject is active (the setting function doesn’t have a return type so it returns “void”, hence the error, using a void and expecting a boolean)
you need to use “activeSelf” or “activeInHierarchy” to check if the gameobject is currently active
http://docs.unity3d.com/ScriptReference/GameObject.html
I don’t quite understand. That’s the same thing I added to my script, and the link didn’t work.
LeftyRighty:
SetActive changes the “active” value you can’t use that to check if the gameobject is active (the setting function doesn’t have a return type so it returns “void”, hence the error, using a void and expecting a boolean)
you need to use “activeSelf” or “activeInHierarchy” to check if the gameobject is currently active
http://docs.unity3d.com/ScriptReference/GameObject.html
Okay, I checked it out, and I couldn’t find any examples of a script. Could you show me what I’d have to do?
none.SetActive will set the activity of the GameObject, not retrieve it, its return type is void , not bool .
is very detailed concerning the use of this property.
if (none.activeSelf)
{
}
will execute just fine.