- Home /
Question is off-topic or not relevant
How to hide a text and show another when Space bar is clicked?,How to hide text after pressing space?
void Update()
{
if (active==true)
{
Motion();
Rotate();
}
else
{
}
// Below is set of lines to be displayed once player face them
var fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, out hit, Reach) && hit.transform.tag == "Bed")
{
interaction.SetActive(true);
if (Input.GetKey(KeyCode.Space))
{
bedLine.SetActive(true);
}
}
else if (Physics.Raycast(transform.position, fwd, out hit, Reach) && hit.transform.tag == "Toilet")
{
interaction.SetActive(true);
if (Input.GetKey(KeyCode.Space))
{
toiletLine.SetActive(true);
}
}
else if (Physics.Raycast(transform.position, fwd, out hit, Reach) && hit.transform.tag == "Speaker")
{
interaction.SetActive(true);
if (Input.GetKey(KeyCode.Space))
{
speakerLine.SetActive(true);
interaction.SetActive(false);
}
}
So, in the second "else if", I would like for the interaction line to hide and speaker line to show. I tried to include another if statement but it didn't change anything. Thus, in my game, whenever I go near the object with speaker tag, interaction appears as it should but when i press space bar the interaction line hide and shows resulting in showing both lines.
by the way I'm 2 weeks old with Unity :)
Answer by mxoconnell · Nov 15, 2019 at 03:07 PM
Move this line
Input.GetKey(KeyCode.Space)
To the top of your code and capture it in a boolean value that you can reference later. Or better yet, have a function called CollectInput that checks that stuff for you. See if it is correctly firing by printing its value.
Forgive my lack of Unity knowledge. Would you $$anonymous$$d copying my script with the changes you pointed at? Thanks a lot.
$$anonymous$$y basic thinking here is that the get key method is only going to fire if the key is being pressed that frame, I think you should break this function up. there is too much happening in the update function, could you change this code so that there are different functions which each have their own role? $$anonymous$$aybe one function can deter$$anonymous$$e if the key is pressed down and save that as a variable which other functions can reference.
Follow this Question
Related Questions
Can't seem to get bool/SetActive Script to work 2 Answers
physics.OverlapSphere colliders 1 Answer
Screensaver when device is idle 1 Answer
Click and drag an object if holding a button down. 0 Answers
How can I enable an object in js? 3 Answers