Tahrim
Tahrim

Reputation: 21

'public' is not valid for this item

Assets\GameControl2D.cs(25,10): error CS0106: The modifier 'public' is not valid for this item

This error is happening why? Please anyone let me help it to figure out.

using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
public class GameControl2D : MonoBehaviour
{
    public static GameControl2D instance;
    public GameObject gameOverText;
    public bool gameOver = false;

    void Awake ()
    {
        if (instance == null)
        {
            instance = this;
        }
        else if (instance != this)
        {
            Destroy (gameObject);
        }
    }

    void Update ()
    {
         public void BirdDied()
        {
            GameOverText.SetActive (true);
            gameOver = true;
        }
    }
   
}

Upvotes: 2

Views: 168

Answers (2)

Tahrim
Tahrim

Reputation: 21

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

public class Bird2D : MonoBehaviour
{
    public float upForce = 200f;
    private bool isDead = false;
    private Rigidbody2D rb2d;

    private Animator anim;

    // Start is called before the first frame update
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (isDead == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                rb2d.velocity = Vector2.zero;
                rb2d.AddForce(new Vector2(0, upForce));
                anim.SetTrigger("Flap");
            }
        }
    }
    void OnCollisionEnter2D()
    {
        isDead = true;
        anim.SetTrigger("Die");
        GameControl2D.Instance.BirdDied();
    }
}

This is the Bird2D.cs file i already remove public from birdDied method but the error is same

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460238

You have a nested/local method BirdDied, that can't have access modifiers. It's accessible only from the method body of Update anyway. So this compiles:

void Update ()
{
    void BirdDied()
    {
        GameOverText.SetActive (true);
        gameOver = true;
    }
}

But since you don't use it in your code, i doubt that it's what you want.

Upvotes: 3

Related Questions