Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development

Angus Eliott
Angus Eliott
3,793 Points

Gameobject problems

Am trying to make a script which toggles a parent containing four headlight game objects on and off with "e". but despite my efforts, MVS and unity don't like the "Headlights.SetActive = !Headlights.SetActive;" statement. could someone help, please? (the commented parts are my other attempts) `` using UnityEngine;

public class CarHeadlights : MonoBehaviour { [SerializeField] public GameObject Headlights; bool Key; //bool headlights; void Start () {

}
void Update () {
    Key = Input.GetKeyUp("e");
    if (Key == true)
    {
        Headlights.SetActive = !Headlights.SetActive;
    }
    /*
    if (Key == true && headlights == false)
    {
        headlights = (true);
        Headlights.SetActive(true);
    }
    if (Key == true && headlights == true)
    {
        headlights = (false);
        Headlights.SetActive(false);
    }*/
}
/*[SerializeField]
public List<GameObject> Headlights;
bool Key;
private void Update()
{
    Key = Input.GetKeyUp("e");
    if (Key == true)
    {
        Headlights.SetActive(false);
    }
}*/

} ``

1 Answer

Alan Mattanรณ
PLUS
Alan Mattanรณ
Courses Plus Student 12,188 Points

Create a boolean and assign the actual result to it. Also, use the console that helps you with this issue.

take a look of this page: https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

look that gameObject.SetActive(false) in parenthesis. Is not assig by using "=". So use the boolean to store the value of false or to get the opposite value.