Type or namespace defintion or file end expected

hello i got an error that says that 1 or more { are not correct but there are correct if i remove this line of code public float rand_t = (Random.Range(5.0f, 50.0f));
then i dont get the error this is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class spawnmob : MonoBehaviour {

    public Transform enemy;



    // Use this for initialization
    void Start()
    {
        public float rand_t = (Random.Range(5.0f, 50.0f));
    }
   
    // Update is called once per frame
    void Update () {
       
    }
}

pls help
thx for help

You cannot make a variable which is local to a function public/private. It is in local scope and only exists within that function. Did you want:

public float rand_t;

Above the functions and just below your enemy variable with:

rand_t = (Random.Range(5.0f, 50.0f));

In the Start function?

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class spawnmob : MonoBehaviour {

    public Transform enemy;
    public float rand_t;

    void Start()
    {
        rand_t = Random.Range(5.0f, 50.0f);
    }
 
    void Update ()
    {
 
    }
}

You can’t declare it public in the start method.

also

public float rand_t = (Random.Range(5.0f, 50.0f));

should actually be

public float rand_t = Random.Range(5.0f, 50.0f);

You would only encase it like that if you have it nested like this

Vector3 position = new Vector3(Random.Range(-10.0f, 10.0f), 0, Random.Range(-10.0f, 10.0f));

thx for help :slight_smile: