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 ReadyLearnerOne · Apr 05, 2018 at 01:49 AM · collisionlightcommand

Light on contact

Using Unity tutorial I am making a light that turns on and off. It currently is controlled with the space bar. I want to replace the spacebar command as the control. I need to adjust the Update so that the command light turns on when GameObject Sample3 is in contact with GameObject Testbase I want the light to turn on, when contact is broken or occurs with anything else it is off. Can anyone guide me on what to put? Below is the script with spacebar as the command:

using UnityEngine; using System.Collections;

public class light : MonoBehaviour { private Light myLight;

 void Start()
 {
     myLight = GetComponent<Light>();
 }


 void Update()
 {
     if (Input.GetKeyUp(KeyCode.Space))
     {
         myLight.enabled = !myLight.enabled;
     }
 }

}

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
0

Answer by Cornelis-de-Jager · Apr 05, 2018 at 02:02 AM

if by contact you mean touching then you can use OnTrigger events. For the code below to work you will need to have the following setup:

  • Your player must have the tag = 'Player'

  • Your object that is being touched must have a RigidBody and Be a trigger (an option ticked in rigidbody), if you don't want it to move then make it kenematic

     void Start()
      {
          myLight = GetComponent<Light>();
      }
      
          void OnTriggerEnter (Collider col) {
             if (col.tag == 'Player')
                 myLight.enabled = true;
          }
          
           void OnTriggerExit (Collider col) {
             if (col.tag == 'Player')
                 myLight.enabled = false;
          }
    
Comment
Add comment · Show 3 · 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 ReadyLearnerOne · Apr 05, 2018 at 02:27 AM 0
Share

thanks for the help. Trigger box wasn't under rigid body it was under Box Collider. I followed that and made the following code, but it isn't causing the light to flip between active and non:

using UnityEngine; using System.Collections;

public class light : $$anonymous$$onoBehaviour { private Light myLight;

 void Start()
 {
     myLight = GetComponent<Light>();
 }


 void OnTriggerEnter(Collider col)
 {
     if (col.tag == "Player")
         myLight.enabled = true;
 }

 void OnTriggerExit(Collider col)
 {
     if (col.tag == "Player")
         myLight.enabled = false;
 }

}

avatar image Cornelis-de-Jager ReadyLearnerOne · Apr 05, 2018 at 02:30 AM 0
Share

Did you change the Tag of your GameObject Sample3 to Player?

You can use this to test:

  void OnTriggerEnter(Collider col)
  {
         Debug.Log(col.tag+ ": On");
          myLight.enabled = true;
  }
 
  void OnTriggerExit(Collider col)
  {
         Debug.Log(col.tag+ ": Off");
          myLight.enabled = false;
  }
avatar image ReadyLearnerOne Cornelis-de-Jager · Apr 05, 2018 at 02:33 AM 0
Share

I did rename the object. I am new, do I add that debug to a new script or replace the old?

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

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Light Colision 1 Answer

Light rarely turns on 1 Answer

Using shadows as Triggers? 1 Answer

Trouble with light switch 3 Answers

Custom Collision 1 Answer


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