william vinemorris william vinemorris - 6 months ago 35
C# Question

Spawner With error message :member names cannot be the same as their enclosing type

I am making a infinite runner and in the spawn script it always this error message and cant figure out how to fix it : Assets/Scripts/Spawn.cs(16,14): error CS0542: `Spawn.Spawn()': member names cannot be the same as their enclosing type.

using UnityEngine;
using System.Collections;

public class Spawn : MonoBehaviour

public GameObject[] obj;
public float spawnMin = 1f;
public float spawnMax = 2f;

// Use this for initialization
void Start()
// Spawn();

void Spawn()
Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
Invoke("Spawn", Random.Range(spawnMin, spawnMax));


The name of your class is the-same as a another function(Spawn) inside the class. Simply rename your Spawn() function to something else and your problem should be solved. Maybe, make the S in your spawn function name to be lowercase while the one in your script name capitalized?

void spawn () {