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

Beginner question, strange error that makes no sense

Discussion in 'Scripting' started by jammer42777, Mar 29, 2021.

  1. jammer42777

    jammer42777

    Joined:
    Apr 21, 2014
    Posts:
    116
    Hello,
    So I'm making a side scroller, but it's been over a year since I've coded anything.
    I'm checking the code from a tutorial (brackeys)


    I'm at the pulling my hair out phaze and this makes no sense at all.


    here is a copy of the code:


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class SpawnBullet : MonoBehaviour
    {
    // Start is called before the first frame update
    void Start()
    {
    public Transform FirePoint;
    public GameObject BulletPrefab;
    }
    // Update is called once per frame
    void Update()
    {
    if(Input.GetKeyDown("f"))
    {
    Debug.Log("Fire!");
    fireweapon();
    }
    }
    void fireweapon()
    {
    //Bullet Logic
    instantiate(BulletPrefab, FirePoint.position, FirePoint.rotation);
    }
    }
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Please post your code with Code Tags in the future for better readability.

    You cannot have variables with access modifiers such as "public" inside of a method, as you do in Start().

    Also, those variables are used elsewhere in the script, so most likely you want to just pull those variables out of the method, so they are instance variables.

    Inside fireweapon(), you have simply misspelled "instantiate", which should be "Instantiate".
     
    Last edited: Mar 29, 2021
  3. jammer42777

    jammer42777

    Joined:
    Apr 21, 2014
    Posts:
    116
    I've done that, fixed the compile errors,
    Thank you so much!

    I'll be sure to add
    Code (csharp):
    1.  in future
     
    PraetorBlue likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713