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 /
This question was closed Nov 24, 2015 at 11:41 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by AnniJ · Nov 17, 2015 at 01:06 PM · scripting problemuiinstantiatebuttonfunction

Pressing space calls the wrong function

Hi guys,

the game I am woring on has two functions so far: Opening a menu by pressing space and instantiating objects when UI-buttons in the menu are pressed. The script for the menu animation is on the FPS and the scripts for spawning the objects are on an empty game object.

The problem is that when I use the spacebar to open or close the menu it now also spawns objects, even though I didn't press any buttons. This only happens once I pressed a button, not when no buttons were pressed during a testrun.

I don't really see why this happens or where the connection between the spacebar and the buttons is.

Here is the scipt for the menu:

 using UnityEngine;
 using System.Collections;
 
 public class MenuAnimS : MonoBehaviour {
     bool MenuOpen;
     Animator animator1;

     void Start () {
         animator1 = GameObject.Find("Canvas").GetComponent<Animator>();
         animator1.enabled = false;
         MenuOpen = false;
     }
     

     void Update () {
         if (Input.GetKeyUp ("space") && MenuOpen == false) {
             animator1.enabled = true;
             animator1.Play ("MenueAnim");
             MenuOpen = true;
         } else if (Input.GetKeyUp("space") && MenuOpen == true){
             animator1.Play ("MenueAnim2");
             MenuOpen = false;
         }
     }
 }

and this is the script for the buttons:

 using UnityEngine;
 using System.Collections;
 
 public class SR : MonoBehaviour {
 
     private GameObject objCamera;
     private Vector3 SpawnPosition;
     private int DistanceToCamera = 5;
     public Transform creation;
     public Transform player;
     
     void Start () {
         objCamera = (GameObject) GameObject.FindWithTag("MainCamera");
     }
     
 public    void Spwn () {
         SpawnPosition = objCamera.transform.forward * DistanceToCamera + objCamera.transform.position ;
         Instantiate(creation, SpawnPosition, player.transform.rotation);
     }
 }

Does anyone see what I've missed?

Thanks :)

Comment
Add comment · Show 2
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 Dave-Carlile · Nov 17, 2015 at 01:07 PM 0
Share

What code calls Spwn?

avatar image AnniJ Dave-Carlile · Nov 17, 2015 at 01:58 PM 0
Share

Spwn is called by the OnClick() functions on the buttons :)alt text

onclick.png (18.0 kB)

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by troien · Nov 17, 2015 at 02:12 PM

That is because the button is selected (In playmode, click the EventSystem, in the inspector, in the preview (The darker area near the bottom) you should see:

Selected:YourButtonNameHere

And in the standard input module you can see that the submit button is set to "Submit". If you then go to "Edit/Project Settings/Input" you can look at submit (Is there twice by default) and one of them has "alt positive button" set to "space". As you can see, return is the other one that triggers it, aswell as joystick button 0.

So pressing space clicks the button if it is selected. The button is still selected because it was the last button you clicked ;)

I believe setting the Button's navigation to "None" should fix it. (Obviously, if you do that navigation won't work for that button anymore)

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 AnniJ · Nov 24, 2015 at 11:10 AM 0
Share

Amazing! Thank you so much! :D

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Missing function in button on click 1 Answer

Trigger Sound more than Once 0 Answers

An Instantiated UI Button doesn't work; clicking does nothing. 1 Answer

Create a new event for Button UI using script. 1 Answer

Why will my button not let me run a function? 4 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