Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 joplrw10 · Aug 15, 2015 at 03:42 AM · unity 5eventsystem

Problem with holding down UI button to instantiate prefab.

I have a button in my UI that has the following script attached to it.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class LaserControl : MonoBehaviour,IEventSystemHandler {
     
     public GameObject character;
     bool buttonHeld = false;
     public GameObject laser;
     public float laserSpeed;
     public AudioClip laserShot;
     public float firingSpeed;
         
     public void pressed (BaseEventData eventData)
     {
         buttonHeld = true;
     }
     public void notpressed(BaseEventData eventData)
     {
         buttonHeld = false;
     }
     void Start () 
     {
         character = GameObject.FindWithTag ("Player");
     }
     void Update(){
         if (buttonHeld){
             Debug.Log ("click");
             InvokeRepeating ("shootLaser", 0.001f, firingSpeed);
         }
         else{
             Debug.Log ("not click");
             CancelInvoke("shootLaser");
         }
     }
     void shootLaser() {
         GameObject shot = Instantiate (laser, character.transform.position,Quaternion.identity) as GameObject;
         shot.GetComponent<Rigidbody2D>().velocity = new Vector2 (0f, laserSpeed);
         AudioSource.PlayClipAtPoint (laserShot,transform.position);
     }
 }

The issue I am having is that I am trying to instantiate at a given interval when the button is held. When I push the button right now, I am invoking at a massive rate. I had the same code being initialized Input.GetKey(KeyCode.space) and the timing was perfect. But when I introduce events, it stops working correctly, and goes into overdrive.

This is my first time using the IEventSysemHandler. I am using Pointer Down and Pointer Up as my BaseEventData at Runtime only.

Any help would be great! I am at a loss where to go from here.

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

Answer by allenallenallen · Aug 15, 2015 at 03:48 AM

 InvokeRepeating ("shootLaser", 0.001f, firingSpeed);

^That means you're repeatedly calling shootLaser() at fireSpeed. Whatever you declared in the inspector.

But if that's set up all correctly, then it's due to the eventData. Why not use EventTrigger?

https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-events-and-event-triggers

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

BaseEvent Data Not Working for IOS 0 Answers

Enable and Disable PointerEnter EventTrigger on number of Buttons present in a single GameObject MenuButtons 1 Answer

Can't find EventSystemEditor in Unity 5.1.2f1 1 Answer

Send Mouse Event 0 Answers

Change properties of a button by pressing another button in Unity 1 Answer


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