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

C# Invalid token 'void' in class, struct, or interface member declaration

Discussion in '2D' started by CobaltTheFox, Feb 5, 2019.

  1. CobaltTheFox

    CobaltTheFox

    Joined:
    May 29, 2015
    Posts:
    27
    I am trying to make a new class that I can use. I keep getting the error above at compile.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory : MonoBehaviour
    6. {
    7.     public int slot_num;
    8.     public int itemid;
    9.     public int quantity;
    10.     public int[,] slot = new int[0, 0];
    11.     List<int[,]> inventory = new List<int[,]>();
    12.  
    13.     public void Generate_inventory()
    14.     {
    15.         int j;
    16.         for (j = 0; j < slot_num; j++)
    17.         {
    18.             inventory.Add(slot);
    19.             Debug.Log(inventory);
    20.         }
    21.     }
    22. }
    I'm trying to set it to an empty object so that I can call it into my player but I'm hitting a wall with this right now. Does anyone know how to fix this?
     
    gamescoresam and SimonDziak like this.
  2. bentontr

    bentontr

    Joined:
    Jun 9, 2017
    Posts:
    39
    The code you posted looks valid, are you sure the error isn't in another file?
     
  3. CobaltTheFox

    CobaltTheFox

    Joined:
    May 29, 2015
    Posts:
    27
    I found out that when I changed the c# version the error would change/disappear. I need to use C# 7.0+
     
    bentontr likes this.
  4. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    I had the same problem, I had to desinstall Visual studio completely, and reinstall it using Unity Hub. Now it works flawlessly in any C# version.
     
  5. yoavlavs2002

    yoavlavs2002

    Joined:
    Apr 28, 2020
    Posts:
    2
    How I install Visual studio using Unity hub?
     
  6. yoavlavs2002

    yoavlavs2002

    Joined:
    Apr 28, 2020
    Posts:
    2
    How I install Visual studio using Unity hub?
     
  7. DynVmIcz

    DynVmIcz

    Joined:
    Jul 28, 2020
    Posts:
    1
    go to installs, click on three dots on your unity install, and click add modules. Then select what you want from that menu and install