Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Beginner problems with C# (bool variable)

Discussion in 'Scripting' started by taschtasch, May 27, 2015.

  1. taschtasch

    taschtasch

    Joined:
    May 27, 2015
    Posts:
    9
    Hello,
    I need to create an easy unity game and therefore code scripts with c#. Unfortunately I have absolutely no idea about programming.
    So my task is to create a public bool variable on which is saved whether a created door is closed or open.
    Additionally I have to code an if else statement to enable or disable the MeshRenderer and BoxCollider.
    So I started doing something which is of course not working..
    That's my code:


    using UnityEngine;
    using System.Collections;

    public class Opendoor : MonoBehaviour
    {
    public bool offen;

    void start()
    {
    offen = false;
    }
    void Update ()
    {
    if (offen == true)
    {
    this.GetComponent<BoxCollider>.enabled = true;
    this.GetComponent<MeshRenderer>.enabled = true;
    }
    else
    {
    this.GetComponent<BoxCollider>.enabled = false;
    this.GetComponent<MeshRenderer>.enabled = false;
    }
    }

    I'd be very thankful if you could help me.. :)
     
  2. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Opendoor : MonoBehaviour
    5. {
    6.  
    7. public bool offen;
    8.  
    9. private BoxCollider _boxcollider;
    10. private MeshRenderer _meshRenderer;
    11.  
    12. void Awake()
    13. {
    14.     _boxCollider = GetComponent<BoxCollider>();
    15.     _meshRenderer = GetComponent<MeshRenderer>();
    16. }
    17.  
    18. void Update ()
    19. {
    20.     _boxCollider.enabled = offen;
    21.     _meshRenderer.enabled = offen;
    22. }
    23.  
    You can cache your components in private variables to make sure you don't have to call GetComponent every update and you can get rid of the "if" statement by setting "enabled" to whatever the current value of offen is.
     
    taschtasch likes this.
  3. taschtasch

    taschtasch

    Joined:
    May 27, 2015
    Posts:
    9
    Thank you very much!
    Yours is working pretty well, but we have to use the "if" statement! It's for college purposes and our version isn't working..
     
  4. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Your if statement is correct, I think you're just missing your parenthesis after GetComponent<>()
     
    taschtasch likes this.
  5. taschtasch

    taschtasch

    Joined:
    May 27, 2015
    Posts:
    9
    thanksthanksthanks!
     
  6. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    Btw please use this for your code.
    Code (CSharp):
    1.  code here
    Its way easier to read.
    You can select it here
     
    hamsterbytedev likes this.
  7. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353