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 /
  • Help Room /
This question was closed Sep 29, 2019 at 06:07 PM by tormentoarmagedoom for the following reason:

The question is answered, right answer was accepted

avatar image
3
Question by NN10 · Sep 27, 2019 at 06:23 PM · scripting problembuttonscripting beginneractivate

How to activate/deactivate script with one button

Hi everyone! I manage to make this script that when you press the key "e", open a menu and make the player stop moving and the camera stop rotating (deactivate those scripts from other GameObjects).

My problem is, I can make menu open and close with the same button, but can't use the same solution with the scripts (I'm pretty sure it's a simple syntax problem, but can't find the solution.)

Script:

 public class Menu : MonoBehaviour
 {
     public GameObject Panel;
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.E))
         {
             Panel.gameObject.SetActive(!Panel.gameObject.activeSelf);
             GameObject.Find("Camera").GetComponent<CameraFollow>()    **??????????**
             GameObject.Find("Player").GetComponent<PlayerController>()    **??????????**
         }
     }
 }

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 goutham12 · Sep 30, 2019 at 05:11 AM 1
Share

The below answer is same but am kind of lazy person to wirte somany line of code. so here my aproach is

      void DoAction(){
                bool result = Panel.gameObject.activeInHierarchy;
                result = !result;
                Panel.gameObject.SetActive(result);
               GameObject.FindGameObjectWithTag("Camera").GetComponent<CameraFollow> 
                                                                                                                                  ().enable=result;
               GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController> 
                                                                                                                                   ().enable=result;
         }

1 Reply

  • Sort: 
avatar image
4
Best Answer

Answer by tormentoarmagedoom · Sep 28, 2019 at 03:28 PM

Hello!

You are little lost my firend :D

lets see, first, for this:

 Panel.gameObject.SetActive(!Panel.gameObject.activeSelf);

is much better if you just do this

 if (Panel.gameObject.activeInHierarchy) Panel.gameObject.SetActive(false);
 else Panel.gameObject.SetActive(true);

Then, for this 2 lines:

 GameObject.Find("Camera").GetComponent<CameraFollow>()
 GameObject.Find("Player").GetComponent<PlayerController>()

You doing nothing here, just, looking for them, but then waht? you give no "orders". As they are not objects, they are components, you must enable/disable them. AANDDD muuch more important, dont use GameObject.Find() is a very very expensive function (it looks throug aaaalll objects in the scene to find it) is much better to assign a tag to that object and use FindObjectWithTag soooo, your script should finish like this:

  if (Panel.gameObject.activeInHierarchy)
     {
     Panel.gameObject.SetActive(false);
     GameObject.FindGameObjectWithTag("Camera").GetComponent<CameraFollow>().enable=false;
     GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>().enable=false;
      }
  else
      {
       Panel.gameObject.SetActive(true);
       GameObject.FindGameObjectWithTag("Camera").GetComponent<CameraFollow>().enable=true;
       GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>().enable=true;
      }

 REMEMEBR YOU NEED TO ASIGN THE TAGS ! (if dont knwo what im talking, look it in youtube, its 1 minute)

Good luck!

Bye!

Comment
Add comment · Show 2 · 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 NN10 · Sep 29, 2019 at 01:31 PM 0
Share

Thanks a lot! I'm endeed super beginner, learning from youtube and stuff. $$anonymous$$y head it's a mess. I been reading a little about Tags and it just what I needed, great advice!

avatar image tormentoarmagedoom NN10 · Sep 29, 2019 at 06:06 PM 1
Share

You'r welcome.

A lot of us started scripting from 0, dont be afraid of spending time withching tutorials where you understand almost anythig, in the end, you will learn more than expected. (Upvote answer please :D)


I close the post.

Follow this Question

Answers Answers and Comments

280 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Half of my graph on Bolt is grayed out and not working 0 Answers

How to write a script for an existing button 1 Answer

UI button issue with variable + inspector 0 Answers

How do you a attach script + event function to another script during runtime in unity? 0 Answers

I want to create a simple crafting/combining system, but I have no idea how! 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