CS0106 - modifier "public"is not valid for this item

Getting this error while working through a tutorial, new to programming, could use some help. Here is the code:

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

public class Paddle : MonoBehaviour

{

// Start is called before the first frame update
void Start()
{
public void Init(bool isRightPaddle) {

Vector2 pos = Vector2.zero;

if (isRightPaddle)
{
// Place paddle on the right of screen
pos = new Vector2(gameManager.topRight.x, 0);
}
else
{
// Place paddle on the left of screen
pos = new Vector2(gameManager.bottomLeft.x, 0);
}
//update this paddle’s position
transform.position = pos;
}
{

}

}

// Update is called once per frame
void Update()
{

}
}

Second Script;

also having issues with the “Init”

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

public class gameManager : MonoBehaviour
{

public ball ball;

public Paddle paddle;

public static Vector2 bottomLeft;
public static Vector2 topRight;

// Start is called before the first frame update
void Start()
{ // convert screen’s pixel coordinate into games coordinate
bottomLeft = Camera.main.ScreenToWorldPoint(new Vector2(0, 0));
topRight = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));

// Create Ball
Instantiate(ball);

// Create 2 paddles
Paddle paddleLeft = Instantiate(paddle) as Paddle;
Paddle paddleRight = Instantiate(paddle) as Paddle;
paddleLeft.Init(True);
paddleRight.Init(True);

}

// Update is called once per frame
void Update()
{

}
}

1 Like

Use code tags and tell us which line the error indicates.

You have your Init method inside your Start method.


Line 13 is saying “The modifier “public” is not valid for this item.
same line says local function “init” is declared but never used.”

i suspect it has something to do with ‘paddles’ but im new to this so just not sure…

thanks so much for the help

1 Like

Please read this thoroughly.
https://discussions.unity.com/t/481379

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

public class Paddle : MonoBehaviour

{
   

    // Start is called before the first frame update
    void Start()
    {
         public void Init(bool isRightPaddle) {

            Vector2 pos = Vector2.zero;

            if (isRightPaddle)
            {
                // Place paddle on the right of screen
                pos = new Vector2(gameManager.topRight.x, 0);
            }
            else
            {
                // Place paddle on the left of screen
                pos = new Vector2(gameManager.bottomLeft.x, 0);
            }
            //update this paddle's position
            transform.position = pos;
        }
    {
       
    }


}

    // Update is called once per frame
    void Update()
    {
       
    }
}

so if i take the out of , then says the same thing.

Now, move the } from the 35th line to the 13th line. And remove the 30-32nd lines.

Also please, follow along this tutorial C# for absolute beginners by Microsoft:

https://www.youtube.com/watch?v=r_5P6GVYJpY

You won’t go far if you don’t learn the basics, like how to format a valid C# program.

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

public class Paddle : MonoBehaviour

{
   

    // Start is called before the first frame update
    void Start()
    {
         public void Init(bool isRightPaddle) { }

            Vector2 pos = Vector2.zero;

            if (isRightPaddle)
            {
                // Place paddle on the right of screen
                pos = new Vector2(gameManager.topRight.x, 0);
            }
            else
            {
                // Place paddle on the left of screen
                pos = new Vector2(gameManager.bottomLeft.x, 0);
            }
            //update this paddle's position
            transform.position = pos;
        }
   
       
   




    // Update is called once per frame
    void Update()
    {
       
    }
}

still getting the same error…?

ill watch that, thanks.

Really? :frowning:

When someone says move a line to another line it means you push the rest of the code downwards. When someone says take the { and insert it at the end of the 13th line, then you do what you did.

[...]
    // Start is called before the first frame update
    void Start()
    {
    }
      
    public void Init(bool isRightPaddle) {
       Vector2 pos = Vector2.zero;
[...]

oh geez. my fault…

eleminate public of public void Init(bool isRightPaddle) {

2 Likes

this is a post for 2019 that is already resolved should check date on things before posting

1 Like