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. Dismiss Notice

changing the value of a bool from a different script

Discussion in '2D' started by Mark_Wilson, Jul 6, 2014.

  1. Mark_Wilson

    Mark_Wilson

    Joined:
    Jan 6, 2013
    Posts:
    112
    Hi guys, I'm having some trouble with c# scripting; I've read solutions to my problem from other posts but none of them are working for me.

    I basically have a sprite which i'll be using as a button. It's its own object (i.e not attached to another game object) and it has a public Boolean which changes its visibility using renderer.enabled= true/false.

    I also have a collider attached to a character and is set to trigger when my player character hits it.

    When this happens I want the button bool to turn to true and appear on screen. Clearly something is happening because when the two characters collide, the game freezes for a split second and the camera goes a bit weird.

    Here is the code I'm using to try and access the bool from a different c# script -

    using UnityEngine;
    using System.Collections;
    public class ElephantMeet : MonoBehaviour {

    void Start ()
    {
    }

    void FixedUpdate ()
    {
    }
    void OnTriggerEnter2D(Collider2D col)
    {
    // If the elephant hits the trigger...
    if (col.gameObject.tag == "Player") {
    GetComponent<connectVisibility>().visible=true;
    }
    }
    void OnTriggerStay2D(Collider2D col)
    {
    // If the elephant hits the trigger...
    if (col.gameObject.tag == "Player") {
    GetComponent<connectVisibility>().visible=true;
    }
    }
    void OnTriggerExit2d (Collider2D col)
    {
    //if the elephant leaves the trigger...
    if (col.gameObject.tag == "Player"){
    GetComponent<connectVisibility>().visible= false;
    }
    }
    }

    Cheers guys, any help is appreciated.
    Mark
     
  2. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Code (csharp):
    1. public connectVisibility VisibilityScript; //Add the Object from the scene wich has that script attached to it, in to the slot. Just like you would add Transform;
    2.  
    3.  
    4. void OnTriggerEnter2D(Collider2D col)
    5. {
    6. if (col.gameObject.tag == "Player") {
    7. VisibilityScript.visible=true;
    8. }
    9. }
    10. void OnTriggerStay2D(Collider2D col)
    11. {
    12. if (col.gameObject.tag == "Player") {
    13. VisibilityScript.visible=true;
    14. }
    15. }
    16. void OnTriggerExit2d (Collider2D col)
    17. {
    18. if (col.gameObject.tag == "Player"){
    19. VisibilityScript.visible= false;
    20. }
    21. }
    22. }
     
  3. Mark_Wilson

    Mark_Wilson

    Joined:
    Jan 6, 2013
    Posts:
    112
    Sorry, I'm having a bit of trouble understanding what slot you mean?
    My game object which the connectVisibility script is attached too is called connectButton.
    so where and how would I reference this object? the name isn't coming up on the suggestion list when I type.

    Cheers for the help Rutenis
     
  4. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    The slot, its the same as Transform. You know, whenever you write Transform you have to put the GameObject into the slot below script. So writing scripts name is the same thing. First you write its name(class), then you write how its going to be named in your script, so you can use it to acces that script. Maybe i suck at explaning or its difficult to explane. :D

    IF THAT DOESNT WORK, JUST WATCH THIS VIDEO, IT EXPLANES IT VERY WELL AND IT WAS REALLY HELPFULL WHEN I STARTED (WATCH THE WHOLE VIDEO):



    1. public (WRITE SCRIPTS NAME THAT YOURE TRYING TO ACCES) VisibilityScript; //Add the Object from the scene wich has that script attached to it, in to the slot. Just like you would add Transform;


    2. void OnTriggerEnter2D(Collider2D col)
    3. {
    4. if (col.gameObject.tag == "Player") {
    5. VisibilityScript.visible=true;
    6. }
    7. }
    8. void OnTriggerStay2D(Collider2D col)
    9. {
    10. if (col.gameObject.tag == "Player") {
    11. VisibilityScript.visible=true;
    12. }
    13. }
    14. void OnTriggerExit2d (Collider2D col)
    15. {
    16. if (col.gameObject.tag == "Player"){
    17. VisibilityScript.visible= false;
    18. }
    19. }
    20. }
     
    Last edited: Jul 6, 2014
  5. Mark_Wilson

    Mark_Wilson

    Joined:
    Jan 6, 2013
    Posts:
    112
    Ah thats great Rutenis.
    na i'm sure that would make sense to anyone else here.
    i'm just a c# novice :p
    Thank you for the help, gonna watch that video now :D