Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by d20kev, Oct 21, 2019.

  1. d20kev

    d20kev

    Joined:
    Jul 19, 2019
    Posts:
    16
    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()
    {

    }
    }
     
    bayimacan likes this.
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Use code tags and tell us which line the error indicates.
     
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    You have your Init method inside your Start method.
     
  4. d20kev

    d20kev

    Joined:
    Jul 19, 2019
    Posts:
    16
    upload_2019-10-21_15-37-25.png
    Line 13 <public> is saying "The modifier "public" is not valid for this item.
    same line <Init> 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
     
    bayimacan likes this.
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
  6. d20kev

    d20kev

    Joined:
    Jul 19, 2019
    Posts:
    16
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Paddle : MonoBehaviour
    6.  
    7. {
    8.    
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.          public void Init(bool isRightPaddle) {
    14.  
    15.             Vector2 pos = Vector2.zero;
    16.  
    17.             if (isRightPaddle)
    18.             {
    19.                 // Place paddle on the right of screen
    20.                 pos = new Vector2(gameManager.topRight.x, 0);
    21.             }
    22.             else
    23.             {
    24.                 // Place paddle on the left of screen
    25.                 pos = new Vector2(gameManager.bottomLeft.x, 0);
    26.             }
    27.             //update this paddle's position
    28.             transform.position = pos;
    29.         }
    30.     {
    31.        
    32.     }
    33.  
    34.  
    35. }
    36.  
    37.     // Update is called once per frame
    38.     void Update()
    39.     {
    40.        
    41.     }
    42. }
     
  7. d20kev

    d20kev

    Joined:
    Jul 19, 2019
    Posts:
    16
    so if i take the <public void> out of <void start>, then <Start> says the same thing.
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    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:


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

    d20kev

    Joined:
    Jul 19, 2019
    Posts:
    16
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Paddle : MonoBehaviour
    6.  
    7. {
    8.    
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.          public void Init(bool isRightPaddle) { }
    14.  
    15.             Vector2 pos = Vector2.zero;
    16.  
    17.             if (isRightPaddle)
    18.             {
    19.                 // Place paddle on the right of screen
    20.                 pos = new Vector2(gameManager.topRight.x, 0);
    21.             }
    22.             else
    23.             {
    24.                 // Place paddle on the left of screen
    25.                 pos = new Vector2(gameManager.bottomLeft.x, 0);
    26.             }
    27.             //update this paddle's position
    28.             transform.position = pos;
    29.         }
    30.    
    31.        
    32.    
    33.  
    34.  
    35.  
    36.  
    37.     // Update is called once per frame
    38.     void Update()
    39.     {
    40.        
    41.     }
    42. }
     
  10. d20kev

    d20kev

    Joined:
    Jul 19, 2019
    Posts:
    16
    still getting the same error...?
     
  11. d20kev

    d20kev

    Joined:
    Jul 19, 2019
    Posts:
    16
    ill watch that, thanks.
     
  12. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    Really? :(

    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.

    Code (CSharp):
    1. [...]
    2.     // Start is called before the first frame update
    3.     void Start()
    4.     {
    5.     }
    6.      
    7.     public void Init(bool isRightPaddle) {
    8.        Vector2 pos = Vector2.zero;
    9. [...]
     
  13. d20kev

    d20kev

    Joined:
    Jul 19, 2019
    Posts:
    16
    oh geez. my fault...
     
  14. ShadicTV

    ShadicTV

    Joined:
    May 9, 2022
    Posts:
    1
    eleminate public of public void Init(bool isRightPaddle) {
     
    jeniths_006 and leoannalena64 like this.
  15. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    this is a post for 2019 that is already resolved should check date on things before posting
     
    Bunny83 likes this.