Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Blast73 · Oct 11, 2015 at 06:50 PM · scripting problemcanvasenable and disable script

Canvas starts enabled no matter what

I'm using a canvas to show a player in proximity to something that they can interact e.g. "Press 'E' to activate."

The goal is to have that appear onscreen when in a trigger. The problem is that once the game starts the canvas is already enabled even though I set it too false in code or set it to false in the editor or do both. Once the player has stepped into the trigger and leaves it works properly.

 using UnityEngine;
 using System.Collections;

 public class Trigger_test : MonoBehaviour {

 //grab audio
 public AudioSource myAudio;
 public Canvas pressE;
 public Collider myPlayer;

 int x = 0;


 void Start () {
     pressE.enabled = false;
 }

 void Update () {
 }

 void OnTriggerStay (Collider myPlayer){
     if (myPlayer.attachedRigidbody){
         //Debug.Log("Collision works");
 
         if (Input.GetKeyDown("e") && x == 0) {
             Debug.Log ("Pause");
             myAudio.Pause();
             x = 1;
         }
     
         else if (Input.GetKeyDown("e") && x == 1) {
             Debug.Log ("UnPause");
             myAudio.UnPause();
             x = 0;
         }                
     }
 }
 
 void OnTriggerEnter (Collider myPlayer) {
     pressE.enabled = true;
 }

 void OnTriggerExit (Collider myPlayer) {
     pressE.enabled = false;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by meat5000 · Oct 11, 2015 at 06:51 PM

Put a debug.log in your OnTriggerEnter to see that there is something triggerring it to begin with. Perhaps the terrain?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Blast73 · Oct 11, 2015 at 06:57 PM 0
Share

Looks like that is the problem. The plane I have everything sitting on is triggering it, but I don't know why. I call the Collider on the firstpersoncontroller so shouldn't only that trigger it?

avatar image meat5000 ♦ Blast73 · Oct 11, 2015 at 07:01 PM 0
Share

Check out the collision matrix in the Physics manager.

Also add a condition in to your OnTrigger routine that check which object has triggered it.

 void OnTriggerEnter (Collider myPlayer) {
     if(myPlayer.gameObject.name == "Whatever")
     {
         press$$anonymous$$enabled = true; 
     }
 }

You get the idea. There are many ways you can make this work, doesnt have to check for the name.

avatar image Blast73 meat5000 ♦ · Oct 11, 2015 at 07:02 PM 0
Share

Thanks for the help

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how to enable or disable a script via code c# 1 Answer

Line Renderer and Canvas Group? 1 Answer

A Canvas inside a Canvas for the purpose of disabling and enabling menus 1 Answer

How to display a message from socket (python) on the screen (Unity) 0 Answers

How to rotate GUI image on cursor drag? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges