0

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();
        }
    }
}
EverettRike
  • 25
  • 1
  • 5
  • BTW if you call `Camera.main` often this is can be a real performance hit, cache your camera somewhere instead. – Omar Abdel Bari Nov 13 '21 at 22:39
  • 1
    Because you are using `private` modifier for a method which is enclosed within the scope of a method. This is a violation., remove private, it is already only accessible within `Start`. Unless that was not intended. In which case remove it from within `Start` and place it after. – Omar Abdel Bari Nov 13 '21 at 22:41
  • Does this answer your question? [Method Within A Method](https://stackoverflow.com/questions/8135050/method-within-a-method) – Omar Abdel Bari Nov 13 '21 at 22:45
  • Omar i believe they did a lot to rectify that hit of Camera.main in new versions. – BugFinder Nov 14 '21 at 00:05
  • According to their [documentation](https://docs.unity3d.com/2021.2/Documentation/ScriptReference/Camera-main.html) They still suggest caching this property. @BugFinder – Omar Abdel Bari Nov 14 '21 at 01:57
  • Yes Omar. But they very recently announced that they basically fixed that. – BugFinder Nov 14 '21 at 09:50
  • It was never broken per say, but perhaps they did some caching themselves to save us the headache. Which would be great but until the documentation is updated or see source code showing the improvement I will be skeptical of this. – Omar Abdel Bari Nov 14 '21 at 12:21

2 Answers2

3

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.
Omar Abdel Bari
  • 934
  • 9
  • 17
-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.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – MD. RAKIB HASAN Jul 04 '22 at 09:59