The question is answered, right answer was accepted
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>() **??????????**
}
}
}
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;
}
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!
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!
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
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