Search Unity

please help i'm kinda confused

Discussion in '2D' started by nelso52879, Feb 11, 2020.

  1. nelso52879

    nelso52879

    Joined:
    Jan 29, 2020
    Posts:
    6
    so im trying to add the Fire 1 so that the player could shoot out a bullet but im having a problem getting it to work the line is if (Input.GetButtonDown ("Fire 1")); and it's not really working. any ideas?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class weapon : MonoBehaviour
    6. {
    7.     public Transform firePoint;
    8.     public int damage = 40;
    9.     public GameObject impactEffect;
    10.  
    11.     // Update is called once per frame
    12.     void Update();
    13.     {
    14.         if(Input.GetButtonDown ("Fire 1"));
    15.      {
    16.         shoot();
    17. }
    18.  
    19.  
    20.  
    21.    void shoot()
    22.    {
    23.        RaycastHit2D hitInfo = Physics2D.Raycast(firePoint.position, firePoint.right);
    24.    
    25.        if (hitInfo)
    26.        {
    27.             Enemy enemy = hitInfo.transform.GetComponent<enemy>();
    28.             if (enemy != null)
    29.             {
    30.             enemy.TakeDamage(damage);
    31.             }
    32.  
    33.         Instantiate(impactEffect, hitInfo.point, Quaternion.identity);
    34.        }
    35.    }
    36.  
    37.  
     
  2. spplayer76

    spplayer76

    Joined:
    Feb 11, 2020
    Posts:
    4
    You have that ; at the end of your if statement. Removing it should fix the problem.
     
  3. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    It's also quite possible that your raycast is hitting the object it's supposed to come out from (or some other incorrect object). I recommend you use Physics2D.RaycastAll() instead of Physics2D.Raycast() and loop through all of the hit objects to see if any of them are enemies.
     
  4. Wezai

    Wezai

    Joined:
    Dec 30, 2016
    Posts:
    74
    You are also missing a curly bracket at line 18. I recommend looking for errors in your code editor and Unity's console, those type of mistakes are really easy to fix alone.

    Debug.Log() is your friend too, it can help check what code blocks are going through or not, making it easier for you to pinpoint what could be the issue.
     
  5. nelso52879

    nelso52879

    Joined:
    Jan 29, 2020
    Posts:
    6
    so I did what you said and it did not really work out.
     
  6. nelso52879

    nelso52879

    Joined:
    Jan 29, 2020
    Posts:
    6
    I looked through all the code (errors) and i found nothing about that its mainly line 14
     
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You've got a semicolon after your Update method definition and your if statement, and you're missing two closing curly braces. You've also defined a variable of type Enemy but then try to assign it using "GetComponent<enemy>". Case matters there.

    Makes sure every single opening curly brace has a matching closing one. You should observe C# standards and capitalize your class names and function names.

    Here is your code with those issues fixed:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Weapon : MonoBehaviour
    6. {
    7.     public Transform firePoint;
    8.     public int damage = 40;
    9.     public GameObject impactEffect;
    10.  
    11.     // Update is called once per frame
    12.     private void Update()
    13.     {
    14.         if (Input.GetButtonDown("Fire 1"))
    15.         {
    16.             Shoot();
    17.         }
    18.     }
    19.  
    20.     private void Shoot()
    21.     {
    22.         RaycastHit2D hitInfo = Physics2D.Raycast(firePoint.position, firePoint.right);
    23.  
    24.         if (hitInfo.collider != null)
    25.         {
    26.             Enemy enemy = hitInfo.transform.GetComponent<Enemy>();
    27.             if (enemy != null)
    28.             {
    29.                 enemy.TakeDamage(damage);
    30.             }
    31.  
    32.             Instantiate(impactEffect, hitInfo.point, Quaternion.identity);
    33.         }
    34.     }
    35. }
     
  8. nelso52879

    nelso52879

    Joined:
    Jan 29, 2020
    Posts:
    6


    thanks that helped out.