Search Unity

Making a UI Canvas appear when a trigger is entered? {SOLVED}

Discussion in 'Scripting' started by milliehashh, Nov 24, 2015.

  1. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    Hey, im pretty much new to C# scripting and i have to make a small game for class, im hoping to have a canvas appear containing text and buttons when you get a certain distance away from NPCs - i was wondering if there is an easy function or few lines of code that i could use to make a canvas appear when a trigger is entered?

    Thanks in advance for any help! :)
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    have the canvas start off as not enabled.

    grab reference to the canvas gameObject in your script that has the trigger, than OnTriggerEnter run .SetActive(true) on the canvas, than OnTriggerExit run .SetActive(false) on the canvas
     
  3. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    sorry, im pretty new to C# so im not sure what to do, what do you mean by grab reference to the canvas gameObject?
     
  4. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    I think what he means by is doing something like this.

    Code (CSharp):
    1. Public Canvas myCanvas; //Your target for the refference
    2.  
    3. Void OnTriggerEnter(collider :Collider){
    4. myCanvas.enabled = true;
    5. }
    6.  
    7. Void OnTriggerExit(){
    8. myCanvas.enabled = false;
    9. }
    10.  
    11. //If you want to be more specific to what gets enabled and store it all in one script you can check tags
    12.  
    13. Void OnTriggerEnter(collider :Collider){
    14.      if (collider.tag =="NPCName"){
    15.            myCanvas.enabled = true;
    16.      }
    17. }
    18.  
    That would be more or less how I would do it.
     
    Last edited: Nov 24, 2015
  5. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    hey, sorry about this, im getting this error:
    Assets/conversationtrigger.cs(6,30): error CS1519: Unexpected symbol `myCanvas' in class, struct, or interface member declaration

    i know its probably caused by something silly like a missing semi-colon but im not sure what it is :( thank you for all the help!!
     
  6. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    I figured out what the problems were for my last reply, the capital P for public wasn't working with unity for me and there was a '}' missing but now i am getting this error in both lines 9 and 13 of the code.

    error CS1061: Type `UnityEngine.Canvas' does not contain a definition for `enable' and no extension method `enable' of type `UnityEngine.Canvas' could be found (are you missing a using directive or an assembly reference?)

    any idea whats causing this, im completely lost from it :(
     
  7. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    Try changing "enable" to "enabled" also make sure at the top of the script you are Using UnityEngine.UI; that should fix the problem.
     
  8. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    That worked a charm, thank you, i knew it would be something silly and little haha. i probably wont have many questions to ask after this but is there any way for me to freeze the players camera but not the cursor once the trigger is entered? thank you for all the help.
     
  9. apsdsm

    apsdsm

    Joined:
    Sep 26, 2013
    Posts:
    56
    I'm sure there is, but how you do it depends on how your camera is being moved around. If you post code we'll be able to help.
     
  10. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    What kind of controller are you using?FPS? If so are you using a Mouselook script?

    Edit: ^ What he said. I think the best way to achieve this is to disable the script when the UI is opened though we might need to have a look first.
     
  11. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    im just using the regular fps camera that you get with the standard unity characters package, i don't have any sort of script for it besides the script that is applied to the fps by default. :)
     
  12. apsdsm

    apsdsm

    Joined:
    Sep 26, 2013
    Posts:
    56
    Ok, I took a look at it. The answer is pretty much what DarkBladeNemo said. You need to disable the script which is responsible for getting the player's input to the camera, without disabling the camera itself (otherwise you'd get a blank screen, or some other undesirable side effect).

    The component you want to disable is the `FirstPersonController` monobehaviour on the FPSController game object (assuming this is the game object you're using). You can proof-of-concept this by running your game in the editor, then manually disabling the monobehaviour by unchecking the checkbox next to it's name.

    a simple:

    Code (CSharp):
    1. gameObject.GetComponent<FirstPersonController>().enabled = false;
    on the FPSController will do it in code, or alternatively if you're calling the script from some other object, you'll need to find the FPSController game object some how (there are lots of ways to do this).

    Hope that helps :)
     
  13. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    thanks, this seems pretty simple - ill try it out when i next get change to! :)