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 verschurengiovanni · Apr 21, 2020 at 10:45 PM · scripting problemscripting beginneremission

Emission Script

I'm having a bit of trouble I have two simple scripts.

One, is if I press the space key, the emission turns off and will turn on again if I let go of the space bar

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FlitsUit : MonoBehaviour
 {
     // 
     public Material material;
 
     private void Update()
     {
         if (UnityEngine.Input.GetKey(KeyCode.Space))
         {
             material.DisableKeyword("_EMISSION"); ;
 
         }
         else
         {
 
             material.EnableKeyword("_EMISSION");
         }
     }
 
 }
 
 

The other one is, If I press E, the emission will turn on and If I press D it will turn off.

They both work, but If i combine them, which is needed for the behaviour I want, they "D" Key doesn't work anymore. It will only turn off for a split second and come on again.

The code for the emission script is

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class KopLamp : MonoBehaviour
 {
     public Material material;
 
 
 
 
     void Update()
     {
 
         if (Input.GetKeyDown(KeyCode.E))
         {
             material.EnableKeyword("_EMISSION");
         }
 
         if (Input.GetKeyDown(KeyCode.D))
         {
 
       
             material.DisableKeyword("_EMISSION");
 
 
         }
     }
 }
 
 

Could someone help me with this? Thanks!

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 yummy81 · Apr 22, 2020 at 06:16 AM

The problem lies in the FlitsUit class. Look at the Update. If you hold down Spacebar, the following piece of code evaluates to true every frame:

     if (UnityEngine.Input.GetKey(KeyCode.Space))
     {
         material.DisableKeyword("_EMISSION"); ;
 
     }

But when you stop holding Spacebar down, the following piece of code evaluates to false and is executed every frame:

     else
     {
 
         material.EnableKeyword("_EMISSION");
     }
 

You can create some sort of communication between your scripts (I used bool "boo"), so they could better understand each other's behaviour:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class KopLamp : MonoBehaviour
 {
     public Material material;
     public bool boo;
     
     void Update(){
         if (Input.GetKeyDown(KeyCode.E)){
             boo = false;
             //material.EnableKeyword("_EMISSION");
         }
         
         if (Input.GetKeyDown(KeyCode.D)){
             boo = true;
             //material.DisableKeyword("_EMISSION");
         }
     }
 }

and the second script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FlitsUit : MonoBehaviour
 {
     public Material material;
     KopLamp kopLamp;
     
     void Awake(){
         kopLamp = FindObjectOfType<KopLamp>();
     }
     
     void Update(){
         if (Input.GetKey(KeyCode.Space)){
             material.DisableKeyword("_EMISSION");
         } else {
             if (kopLamp.boo){
                 material.DisableKeyword("_EMISSION");
             } else {
                 material.EnableKeyword("_EMISSION");
             }
         }
     }
 }


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 verschurengiovanni · Apr 22, 2020 at 03:57 PM 0
Share

Thanks! That actually did the trick!

Since i'm still learning at this point, I have a other question aswell.

I want to play 2 animations but on the same key press. I made a script, but I think i'm overhtinking it.

The "b" key plays the first animations and if a user presses the key again it plays the seconds animation.

 using UnityEngine;
 using System.Collections;
 
 public class Beugel : $$anonymous$$onoBehaviour
 {
 
     public Animator beugel;
 
     // Use this for initialization
     void Start()
     {
         beugel = GetComponent<Animator>();
     }
 
     // Update is called once per frame
     void Update()
     {
 
         if (Input.GetKeyDown("b"))
         {
             if (Input.GetKey("b"))
             {
                 beugel.Play("Beugel");
             }
             else
             {
                 beugel.Play("BeugelOmlaag");
             }
         }
     }
 }

It plays the first animation just fine, but it wont play the second animation.

I'm not sure what i'm doing wrong here ..

avatar image yummy81 verschurengiovanni · Apr 22, 2020 at 07:08 PM 1
Share

Try this:

 using UnityEngine;
 using System.Collections;
 
 public class Beugel : $$anonymous$$onoBehaviour
 {
     public Animator beugel;
     bool boo;
 
     void Start()
     {
         beugel = GetComponent<Animator>();
     }
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.B))
         {
             boo ^= true;
             
             if (boo){
                 beugel.Play("Beugel");
             } else {
                 beugel.Play("BeugelOmlaag");
             }
         }
     }
 }
 
avatar image verschurengiovanni yummy81 · Apr 23, 2020 at 12:24 PM 0
Share

Thank you so much! This actually fixed my problem!

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

231 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 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

Emission blinking script 1 Answer

Point Counter Works Only Once! 1 Answer

How to double spirte/gameobject/prefab and control the result on those items? 0 Answers

How would I access my list from another function? 1 Answer

Shooting with get joint rotation .eulerAngles kinect 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