EverettRike
EverettRike

Reputation: 25

error CS0106: The modifier 'private' is not valid for this item C# error in Unity

I keep getting this error ( CS0106: The modifier 'private' is not valid for this item) and could use some help. I'm trying to make a random object generator for my game but since I am still a newbie coder I cannot seem to figure out how to fix this. Any help would be nice

Here is the code im using:

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

public class deployAsteroids : MonoBehaviour
{

public GameObject asteroidPrefab;
public float respawnTime = 1.0f;


void Start()
{
    screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(screenBounds.x, screenBounds.y, Camera.main.transform.position.z));
    StartCorountine(asteroidWave());
    
    private void spawnEnemy()
    {
        GameObject a = Instantiate(asteroidPrefab) as GameObject;
        a.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y * -2);
    }
    IEnumerator astroidWave()
    {
        while (true)
        {
            yield return new WaitForSeconds(respawnTime);
            spawnEnemy();
        }
    }
}

Upvotes: 0

Views: 6531

Answers (2)

Person is me
Person is me

Reputation: 1

Try using the static modifier, and reading this. That could work, it worked for me when I got the same error in unity for a public void.

Also, consider moving your method out of Start(), like others have said.

Upvotes: -1

Omar Abdel Bari
Omar Abdel Bari

Reputation: 964

Anytime you get a compiler error the first thing you should consider is to lookup up the error code in a search engine. From CS0106 the rules are provided. The forth bullet point is clear on this.

Access modifiers are not allowed on a local function. Local functions are always private.

You have two options:

  1. Move the method outside of the parent method (Start()). This is the typical scenario if it will be used in more than one place.
  2. Remove the modifier private.

Upvotes: 2

Related Questions