Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
-1
Question by 14ercooper · Aug 13, 2015 at 09:59 PM · 3dlightswitch

Switch between two light sources

I need a script to switch between two light sources, both children of the player. I have a code that can tell if it is time to switch the light sources, and to reset the variable after switching the lights. I just don't know how to activate/deactive the game objects of the lights. The light sources are a sun and a torch.

 using UnityEngine;
 using System.Collections;
 
 public class LightSourceManager : MonoBehaviour {
 
     public bool switchLights = false;
 
     void Update () {
         if (switchLights) {
             lightSwap ();
             switchLights = false;
         }
     }
 
     private void lightSwap () {
         //Put code here
     }
 }
 
Comment
Add comment · Show 1
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 14ercooper · Aug 13, 2015 at 10:50 PM 0
Share

What is the code to toggle if a game object is active or not?

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Nirvana33 · Aug 13, 2015 at 10:54 PM

Try

  if (light1.enabled==false){
           light1.enabled=true;
           light2.enabled=false;
           }
           else{
           light2.enabled=true;
           light1.enabled=false;
           }

Comment
Add comment · Show 6 · 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 14ercooper · Aug 13, 2015 at 11:18 PM 0
Share

How do I get the references for light1 and light2?

avatar image Nirvana33 · Aug 13, 2015 at 11:43 PM 0
Share

like that:

 public Light light1;
 public Light light2;
avatar image 14ercooper · Aug 14, 2015 at 12:04 AM 0
Share

I get the following error, and all of my scripts are disabled and I can't do anything with scripts: Assets/Scripts/Player$$anonymous$$anagers/LightSource$$anonymous$$anager.cs(30,31): error CS1061: Type UnityEngine.Light' does not contain a definition for Enabled' and no extension method Enabled' of type UnityEngine.Light' could be found (are you missing a using directive or an assembly reference?)

avatar image Nirvana33 · Aug 14, 2015 at 01:40 AM 0
Share

Do you have 2 different gameobject and you want to change between them? If so, code have to work but I guess they have to be

 if (light1.enabled==false){
          light1.enabled=true;
          light2.enabled=false;
          }
          else{
          light2.enabled=true;
          light1.enabled=false;
          }

Not Enabled, enabled I'm sorry, I typed wrong

avatar image 14ercooper · Aug 14, 2015 at 02:20 AM 0
Share

That almost works, except the only light source that creates light is the one that was enabled at the start. The other one doesn't emit light.

Show more comments
avatar image
1

Answer by Pontik · Aug 14, 2015 at 03:31 AM

     public Light light1, light2;
 
     public void switchLights()
     {
         if(light1 && light2)
         {
             if (light1.gameObject.activeInHierarchy)
             {
                 light1.gameObject.SetActive(false);
                 light2.gameObject.SetActive(true);
             }
             else
             {
                 light2.gameObject.SetActive(false);
                 light1.gameObject.SetActive(true);
             }
         }
     }
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 14ercooper · Aug 14, 2015 at 03:42 AM 0
Share

I solved this about 10 seconds before I saw your comment, but thanks :D

avatar image
0
Wiki

Answer by starikcetin · Aug 13, 2015 at 10:52 PM

http://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

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 14ercooper · Aug 13, 2015 at 11:18 PM 0
Share

I need to be able to switch between the two easily.

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

27 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

Related Questions

Light rarely turns on 1 Answer

c# light off 2 Answers

My lighting is weird 1 Answer

Converting a surface shader & materials to work with URP - help?,Having trouble converting materials & surface shader to work with URP - Help? 0 Answers

Unity 3D - Lighting - Torches - HELP! 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