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 /
avatar image
0
Question by Reien · Mar 17, 2018 at 10:12 AM · instantiatetransform.positionmobile devicesgui-button

transform of instantiated bullet via ui button press not following the player

Problem: When a button is pressed, the bullet instantiates at the starting position of the bullet position that is a Child of my player even after the player moves. The shooting works just fine when I press spacebar to fire, but when I created a different function for UI button press the bulletposition's transform doesn't update. This is a snippet from my player script:

 using UnityEngine;
 using UnityEngine.UI;
 public class PlayerControl : MonoBehaviour {
 
     public GameObject PlayerBulletGO;
     public GameObject bulletPosition;    
     public GameObject ExplosionPrefab;

 public float FireRate;
 private float canFire;

 
     void Update () {
 
         FireBullets();
     }
 
 // bulletPosition updates properly and works as intended
     public void FireBullets()
     {
             // Fire bullets when spacebar is pressed
         if(Input.GetButton("Jump") && canFire <= 0)
         {
             // Instantiate the bullet
             GameObject bullet = (GameObject)Instantiate(PlayerBulletGO);
             bullet.transform.position = bulletPosition.transform.position;
             canFire = FireRate;
         }
         else
         {
             canFire -= Time.deltaTime;
         }
     }
 
 // bulletPosition stay where player is instantiated at the start of the game
     public void FireBulletsTouch()
     {
             // Fire bullets when spacebar is pressed
         if(canFire <= 0)
         {
             // Instantiate the bullet
             GameObject bullet = (GameObject)Instantiate(PlayerBulletGO);
             bullet.transform.position = bulletPosition.transform.position;
             canFire = FireRate;
         }
         else
         {
             canFire -= Time.deltaTime;
         }
     }
 }

This is the whole script of the UI button for shooting.

 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class ButtonHold : MonoBehaviour {
 
     public Button fireButton;
     public PlayerControl playerShoot;
 
     public bool isHeld;
 
     void Update()
     {
         if(isHeld)
         {
             playerShoot.FireBulletsTouch();
         }
     }
 
      public void OnPointerDown()
      {
          isHeld = true;
      }
      public void OnPointerUp()
      {
          isHeld = false;
      }
 }
 

I have tried various things such as the one without event trigger but with PointerEventData and all but the outcome is still the same. At first the script for UI button shooting was inside the PlayerControl script too but since I can't fix the problem and also the script is getting messier I decided to create a new script for the button which just gives the same outcome.

Also I would like to know a better way of making it possible for the player to shoot on normal button tap and while on hold. Please help me with this one. Thank you!

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

0 Replies

· Add your reply
  • Sort: 

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

94 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

Related Questions

GameObject change Position after game started 1 Answer

transform.position on Instaniated object does not match inspector 0 Answers

How do I instantiate on the game object's position that i put the script on 1 Answer

Instanitiate PreFabs and let them move to different locations 1 Answer

Why can't I get the transform from an instantiated object? 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