Search Unity

The script don't generate projectile in unity?

Discussion in 'Scripting' started by huseyinbaba58, Aug 5, 2020.

  1. huseyinbaba58

    huseyinbaba58

    Joined:
    Feb 12, 2020
    Posts:
    146
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HelikopterSaldirisi : MonoBehaviour
    6. {
    7.     public Transform hedef;//Hedef nesne belirlendi.
    8.     public GameObject mermi;//Mermi belirlendi.
    9.     public AudioSource sesKaynagi;
    10.     void Awake()
    11.     {
    12.         sesKaynagi = GetComponent<AudioSource>(); //Ses kaynağını da ekledik.
    13.     }
    14.     void Update()
    15.     {
    16.         if(Input.GetKey(KeyCode.F))
    17.         {
    18.             Atis();
    19.         }
    20.     }
    21.     void Atis()
    22.     {
    23.         if (mermi != null)
    24.         {
    25.             Vector3 yon = (hedef.position - transform.position);
    26.             float k = (yon.x / yon.z); //yatay eksen ile düşey eksenin eğimi hesaplandı.
    27.  
    28.             float donus = Mathf.Atan(k) * Mathf.Rad2Deg;
    29.             GameObject mermiler = Instantiate(mermi, transform.position, Quaternion.identity) as GameObject; //Örneklendirme yapıldı.
    30.             mermiler.transform.rotation = Quaternion.Euler(0f, donus, 0f); //Böyle yönelecek.
    31.             mermiler.GetComponent<Rigidbody>().AddForce(yon * 5.0f);
    32.  
    33.             Destroy(mermiler, 2f); //2 saniye sonra yok olacak.
    34.             sesKaynagi.Play();//Ses kaynağı çalıştırılacak.
    35.         }
    36.     }
    37.    
    38.    
    39. }
    40.  
    What is this error causing?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    * Are you getting an error in the console?
    * Are you sure the script is attached, and the "mermi" variable is assigned to a prefab?
    * Does the AudioSource successfully play its sound?
     
  3. huseyinbaba58

    huseyinbaba58

    Joined:
    Feb 12, 2020
    Posts:
    146
    I didn't take an error message.But Game object isn't copied.
     
  4. mgrekt

    mgrekt

    Joined:
    Jun 22, 2019
    Posts:
    92
    Could be that mermi equals to null.