Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 latsushi · Jun 14, 2012 at 05:13 AM · c#lightoncollisionenterfindaddcomponent

Turning a Light on With Collision Detection

I am trying to write a script whereby when a cube lands on the floor the light turns on. I am working in C#, not in Javascript.

public class lighton : MonoBehaviour {

 void OnCollisionEnter (Collision myCollision) {
     
     Light myLight = gameObject.Find("Light").AddComponent(Light);
         myLight.enabled = true;
         myLight.intensity = 5;
 
 }

}

I get an error that says:

Static member `UnityEngine.GameObject.Find(string)' cannot be accessed with an instance reference, qualify it with a type name instead

Any input is appreciated

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
Best Answer

Answer by fafase · Jun 14, 2012 at 06:12 AM

OK I removed my former answer, my bad I got mixed up between Js and C#

 public class lighton : MonoBehaviour {
     public Light myLight; 
     void Start () {
          GameObject light = GameObject.FindWithTag("Light");
         myLight = light.GetComponent<Light>();
         myLight.enabled = false;
     }    
     void Update () {
         if(Input.GetKeyDown(KeyCode.Space))
             myLight.enabled = !myLight.enabled;            
         }
 }

It works for me, I can switch my light on and off. All you need now is to convert it into a collision as I am using a button to switch.

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 latsushi · Jun 14, 2012 at 06:31 AM 0
Share

Thank you very much kind person :-) It's working. Below is my final code I used to get a light turn on as soon as a cube collided with something.

public class lighton : $$anonymous$$onoBehaviour {

 public Light myLight; 
 void OnCollisionEnter (Collision myCollision) {
    GameObject light = GameObject.FindWithTag("Light");
    myLight = light.GetComponent<Light>();
    myLight.enabled = true;
 } 

}

*(A note to everyone)Remember to rename the tag on your light source to "Light" if you're using this example.

avatar image BlackMashroom latsushi · Nov 21, 2021 at 05:33 PM 0
Share

You are great Latsushi ! I have been looking for this syntax for more than one week - to turn on the light "OnCollisionEnter". Many thanks

avatar image fafase · Jun 14, 2012 at 06:55 AM 0
Share

If your collision happens once in a while, this code is fine, on the other hand if it is likely to happen many times you should cache reference you variable.Lookup functions are expensive and it is better to avoid them. See what I have done in the Start function.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Using OnCollisionEnter with cube 2 Answers

Don't understand why this on collision script doesn't work. 2 Answers

How to find a Class by value 2 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