Search Unity

A namespace cannot directly contain members such as fields or methods

Discussion in 'Getting Started' started by fivedeuce, May 12, 2020.

  1. fivedeuce

    fivedeuce

    Joined:
    May 6, 2020
    Posts:
    1
    Doing a tutorial for creating a game. Very early in and learning about loops. When I try to compile this code, I get an error. I looked at some previous resolutions for this issue and all I saw was that there needed to be a monobehaviour attached and I already have that. Also the file is saved as Player.cs, so unity should be reading it correctly. What do I need to do to make this code compile?

    Unity is telling me it's line 24. That is were 'Void update()' is.



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

    public class Player : MonoBehaviour
    {


    // Start is called before the first frame update
    void Start()
    {
    for (int year = 1991; year <= 2000; year++)

    {
    UnityEngine.Debug.Log(year);
    }


    }
    }

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

    }
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    The Update method is outside of your Player class.
     
  3. Charles-Brant

    Charles-Brant

    Joined:
    May 11, 2020
    Posts:
    9
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Diagnostics;
    5. using UnityEngine;
    6.  
    7. public class Player : MonoBehaviour
    8. {
    9. // Start is called before the first frame update
    10. void Start()
    11. {
    12. for (int year = 1991; year <= 2000; year++)
    13. {
    14. UnityEngine.Debug.Log(year);
    15. }
    16. }
    17.  
    18. // Update is called once per frame
    19. void Update()
    20. {
    21.  
    22. }
    23. }
    24.