Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by NaveGCT · Sep 12, 2020 at 12:47 PM · variablebooleangunbulletsgun script

I've been trying to make a gun script, but somehow the boolean to tell when the bullet is destroyed doesent work

So im trying to make a gun script, and this is the script that is attatched to the bullet. My issue, is that when the bullet hits an enemy, it does not set "hashit" to true. It does detect the collision however.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class bulletSelf : MonoBehaviour
 {
     bool original = true;
     [Header("The speed of the bullet moving forward")]
     public float speed;
     [Header("Do you want the bullet to despawn after a certain amount of time")]
     public bool Despawn;
     [Header("Time before the bullet despawns")]
     public float TimeToDespawn;
     [Header("To detect if this is the original bullet- Dont Change")]
     public OBG obg;
     [Header("Set this variable to itself")]
     public GameObject thisBullet;
     [Header("All tags attatched to enemy")]
     public List<string> AllEnemyTags;
     [Header("The time after the bullet hits an enemy before it despawns (recommended 0)")]
     public float TimeAfterHit;
     bool hasHit;
 
     public void Start()
     {
         if(thisBullet == obg.originalbullet)
         {
             original = true;
         } else
         {
             original = false;
         }
         hasHit = false;
     }
 
     public void OnCollisionEnter(Collision collision)
     {
         Debug.Log("collision detected");
         foreach (string tag in AllEnemyTags)
         {
             Debug.Log(tag + " found");
             if (collision.gameObject.CompareTag(tag))
             {
                 Debug.Log("collision detected with" + collision.gameObject.name);
                 if (TimeAfterHit == 0)
                 {
                     Debug.Log("No time is there!");
                     Destroy(thisBullet.gameObject);
                 }
                 Debug.Log("Setting hashit");
                 hasHit = true;
             }
         }
     }
 
     void Update()
     {
         if (hasHit == true)
         {
             Debug.Log("hashit is true");
             if (TimeAfterHit > 0)
             {
                 Debug.Log("Hashit greater than zero, subtracting");
                 TimeAfterHit = TimeAfterHit - Time.deltaTime;
             }
             if (TimeAfterHit < 0)
             {
                 Debug.Log("Hashit less than zero, setting to zero");
                 TimeAfterHit = 0;
             }
             if (TimeAfterHit == 0)
             {
                 Debug.Log("destroying");
                 Destroy(thisBullet.gameObject);
             }
         }
         if (original == false)
         {
             transform.Translate(Vector3.forward * speed * Time.deltaTime);
             if (Despawn == true)
             {
                 TimeToDespawn = TimeToDespawn - Time.deltaTime;
                 if (TimeToDespawn < 0)
                 {
                     TimeToDespawn = 0;
                 }
                 if (TimeToDespawn == 0)
                 {
                     Destroy(thisBullet.gameObject);
                 }
             }
         }
     }
 }
 


Here is what the console logs say: Collision Detected Enemy Found Collsiion detected withEnemy setting hashit Then nothing else; after that it SHOULD have set hashit to true, but it doesent

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 Siliko · Sep 12, 2020 at 01:03 PM

Hello,

Maybe try initiating hasHit as false when you declared it first and not at start() ?

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

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

Boolean variable won't trigger in other script 1 Answer

2D gun aiming problem 1 Answer

How to keep a variable from changing? 2 Answers

Need help with shooting and reload script 1 Answer

Rotation Flicker in 2d platformer gun 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