Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why wont this print?

Discussion in 'Scripting' started by epochplus5, Sep 25, 2021.

  1. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    hi guys,

    Trying to deternate a shell after it has been fired from my tank
    it really looks like it should be working but the button wont trigger the print caption
    i just cant get print("det flak"); to print
    the true false is working and i can even get into the button function but it just isnt seeing the true or false....please help

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerTankShell : MonoBehaviour
    6. {
    7.     public ParticleSystem shellExplodes;
    8.     public AudioSource shellExplodesSound;
    9.     public DamageController damageController;
    10.  
    11.     public Collider2D blastRadius;
    12.  
    13.     private float timer;
    14.     private bool shellCanDetonate = false;
    15.  
    16.     private void Start()
    17.     {
    18.         damageController = FindObjectOfType<DamageController>();
    19.      
    20.     }
    21.  
    22.     private void Update()
    23.     {
    24.         CheckShellcanDetonate();
    25.         print(shellCanDetonate);
    26.     }
    27.     private void OnTriggerEnter2D(Collider2D collider)
    28.     {
    29.         if (collider.gameObject.tag == "Player")
    30.         {
    31.             damageController.TakingDamage(10);
    32.         }
    33.      
    34.         Instantiate(shellExplodes, transform.position, transform.rotation);
    35.         shellExplodesSound.Play();
    36.         Destroy(gameObject, 0.1f);
    37.         blastRadius.enabled = true;
    38.         StartCoroutine(BlastRadius());
    39.     }
    40.  
    41.     void CheckShellcanDetonate()
    42.     {
    43.         timer += Time.deltaTime;
    44.      
    45.         if (timer > 3)
    46.         {
    47.             shellCanDetonate = true;
    48.         }
    49.     }
    50.  
    51.     public void FlackDetonate()
    52.     {
    53.         if (shellCanDetonate)
    54.         {
    55.             print("det flak");
    56.         }
    57.     }
    58.     IEnumerator BlastRadius()
    59.     {
    60.         float waiting = 0.2f;
    61.         yield return new WaitForSeconds(waiting);
    62.         blastRadius.enabled = false;
    63.     }
    64. }
    65.  
    66.  
    67.  
     
  2. Pandazole

    Pandazole

    Joined:
    Sep 23, 2019
    Posts:
    15
    Who is calling the "FlackDetonate()" method?
     
  3. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    hi Pandazolie,

    its a button in my UI connected by click to that method.
     
  4. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    So do methods on scripts that are connected to prefabs still run even if the object is not instantiated? i destroy my shell when it hits the ground but the method still prints allthough the object the scrip is attached to has been destroyed.