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 IchabodCraneThe3rd · Mar 10, 2019 at 11:29 PM · prefabcontrollercontrolsgetkeydowninconsistent

Why is GetKeyDown instantiating my prefab inconsistently c#

@crazyKnight I am making a game where the main character (Alva) can shoot bullets and I am using a prefab to make a new bullet every time the player presses "e", the hit timer is > 100 (this makes it so the player can only swing their sword or shoot every so often) and the bullet range timer is 0 (this timer sets the range for the bullets. when the timer is = 100 the bullet is destroyed). but when I press "E" only about 1 in 10 times does Alva shoot a bullet. This is my bullet script:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Bullet : MonoBehaviour

{

 public float xSpeed;

 public float ySpeed;

 GameObject BulletClone;



 void Start()
 {
     CheckFireDirection();
     GameObject Alva = GameObject.Find("Alva");
     position = this.GetComponent<Transform>();
     transform.position = Alva.transform.position;
 }
 void Move()
 {
     transform.Translate(xSpeed * Time.deltaTime, ySpeed * Time.deltaTime, 0);

 }

 void CheckFireDirection()
 {
     GameObject Alva = GameObject.Find("Alva");
     PlayerController alva = Alva.GetComponent<PlayerController>();
     if (this.name != "Bullet_Prefab")
     {
         if (alva.faceUp)
         {
             ySpeed = 15;
             xSpeed = 0;
         }
         else if (alva.faceDown)
         {
             ySpeed = -15;
             xSpeed = 0;
         }
         else
         {
             ySpeed = 0;
         }
         if (alva.faceRight)
         {
             xSpeed = 15;
             ySpeed = 0;
         }
         else if (alva.faceLeft)
         {
             xSpeed = -15;
             ySpeed = 0;
         }
         else
         {
             xSpeed = 0;
         }
     }
 }

 void CheckRangeTimer()
 {
     GameObject Alva = GameObject.Find("Alva");
     PlayerController alva = Alva.GetComponent<PlayerController>();
     if (alva.bulletRangeTimer == 100 && this.name != "Bullet_Prefab")
     {
         Destroy(this.gameObject);
     }
 }

 void CheckInstantiate()
 {
     GameObject Alva = GameObject.Find("Alva");
     PlayerController alva = Alva.GetComponent<PlayerController>();
     GameObject Bullet_Prefab = GameObject.Find("Bullet_Prefab");
     if (Input.GetKeyDown("e") && alva.hitTimer >= 100 && alva.bulletRangeTimer == 0)
     {
         BulletClone = Instantiate(Bullet_Prefab, Alva.transform.position, Quaternion.identity) as GameObject;
          alva.hitTimer = 0;
     }
     
 }

    void Update()
 {
     CheckInstantiate();
     CheckRangeTimer();
     Move();
 }

}

This is where I define the bullet range timer and the hit timer

int bulletRangeTimer; int hitTimer;

 void Move()
 {
    if (Input.GetKeyDown("e") && hitTimer >= 100 && bulletRangeTimer == 0)
         {
             hitTimer = 0;
         }
     }

void CheckBulletTimer()

 {

     if (Input.GetKeyDown("e") && bulletRangeTimer == 0)

     {

         bulletRangeTimer = 1;

     }

     if (bulletRangeTimer != 0)

     {

         bulletRangeTimer++;

     }

     if (bulletRangeTimer > 100)

     {

         bulletRangeTimer = 0;

     }

 }

void Update(){

 hitTimer++

 CheckBulletTimer();

}

Sorry about so much code. this is just a very broad issue so it is important to know everything about it.

If you can help me in any way that would be greatly appreciated

Michael

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
Best Answer

Answer by WarmedxMints · Mar 11, 2019 at 12:03 AM

The problem appears to be that you are adding to bulletRangeTimer each frame. So unless you happen to press e on that frame, the statement bulletRangeTimer == 0 is going to return false and the code in that if statements block will not run.


On another note, GameObject.Find is not a quick method, do not perform it each frame. Get a reference to the PlayerController like so;

     private PlayerController _alva;
 
     private void Start()
     {
         _alva = GameObject.Find("Alva").GetComponent<PlayerController>();
         transform.position = _alva.transform.position;
         ///


Then you can do this;

     void CheckRangeTimer()
     {
         if (_alva.bulletRangeTimer == 100 && this.name != "Bullet_Prefab")
         {
             Destroy(this.gameObject);
         }
     }

Although, you could just make it public or a serialized field and populate it in the inspector.

Also, why are you checking if the gameobject the script is on isn't called Bullet_Prefab?

Comment
Add comment · Show 2 · 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 IchabodCraneThe3rd · Mar 11, 2019 at 12:17 AM 0
Share

I am checking it because I have this script on the bullet prefab and the children so I don't want the original prefab to be destroyed.

thank you for your help

avatar image WarmedxMints IchabodCraneThe3rd · Mar 11, 2019 at 07:38 AM 0
Share

The bullet prefab should be in your project folder. There is no need to keep it in the scene nor have this script on it tbh. It should have its own class.

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

134 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 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

Best use of GetButton 4 Answers

Changing controls from keyboard to touch in the game 0 Answers

How to give an image to a sprite in c# code? 1 Answer

Best Practice Multiple Control Methods 1 Answer

how to check wich player uses wich controller/ keyboard and how to assign these when switching scenes 0 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