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 scalliontang · Mar 19, 2017 at 08:47 PM · lightintensity

Error CS(11,10)an object reference is required non static member 'UnityEngine.Light.intensity‘

Hi, I want to write a script to control the light. When the player walks into a certain area and the light turns on and when they leave the light turns off. Here is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class On : MonoBehaviour {
     public float intensity;
     // turn on the light
     public class ExampleClass : MonoBehaviour {
         void OnTriggerEnter(Collider other) {
             Light.intensity = 4.67F;
         }
     }
 
     // Turn off the light
     void OnTriggerExit(Collider other) {
         Light.intensity = 0.00F;
 
     }
 }
 

I just got into coding so I am a little confused.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Commoble · Mar 19, 2017 at 08:50 PM

You need a reference to a specific light component to change the intensity of that component.

You can do that like this:

  public class On : MonoBehaviour {
      public Light lightComponent; // Inspector field
 
      // turn on the light
      public class ExampleClass : MonoBehaviour {
          void OnTriggerEnter(Collider other) {
              lightComponent.intensity = 4.67F;
          }
      }
  
      // Turn off the light
      void OnTriggerExit(Collider other) {
          lightComponent.intensity = 0.00F;
  
      }
  }

This will create a field in your script component's Inspector that you can drag a Light component into, and that Light component will be assigned to the lightComponent field when the scene initialized. You can get more details on this subject here and here.

For more help on fundamentals of using C#, the official scripting tutorials have some explanations.

Comment
Add comment · Show 1 · 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 RobAnthem · Mar 19, 2017 at 08:56 PM 0
Share

Damn... I think your answer was less detailed, but overall cleaner lol, and better because it included a link to some explanations.

avatar image
0

Answer by RobAnthem · Mar 19, 2017 at 08:55 PM

This is a beginners coding problem, essentially what your error is saying is that you don't actually have a reference to an Object.

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class On : MonoBehaviour {
      public float intensity;
      public Light light;
      // turn on the light
      public class ExampleClass : MonoBehaviour {
          void OnTriggerEnter(Collider other) {
              light.intensity = 4.67F;
          }
      }
  
      // Turn off the light
      void OnTriggerExit(Collider other) {
          light.intensity = 0.00F;
  
      }
  }

Would be the proper way, and you would need to either drag and drop the light component from the editor, or run a GetComponent() method if it is attached to the object.

Basically to break it down for you, Light is a Type, you can't reference classes that aren't static as actual objects, because they are simply Types at that point. So you have to have an instance of the type, and reference the instance itself.

for example

 public class Item
 {
     public int count;
 }
 
 public class itemHandler
 {
     public void handleItem()
     {
         Item.count = 5;
     }
 }

See I am only referencing the class of type Item, not an instance of it. So this would be an example of a working version.

     public class Item
     {
         public int count;
     }
     
     public class itemHandler
     {
         public Item item;
         public void handleItem()
         {
            if (item == null)
           {
               item = new Item();
           }
             item .count = 5;
         }
     }
 

I hope this helps.

Comment
Add comment · 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

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

light intensity change with easing problem!! 1 Answer

How to change a light's intensity between two values by pressing a key? 1 Answer

How to prevent Overlapping Lights from combining / adding intensity? 3 Answers

GUI Problem 1 Answer

Light Draining Script 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