- Home /
Struct Error CS0122
I'm testing out Structs in Unity by making a Public Struct containing 3 int variables and then try assigning a value to each in the Start method. The problem is that I am getting a CS0122 error stating "`variable' is inaccessible due to its protection level".
What is the problem here and how can I fix it? I've put the code bellow. Please Help!
Thanks!
Code:
using UnityEngine;
using System.Collections;
using System;
public class Weapons : MonoBehaviour {
public struct WeaponSelect {
int weapon;
int ammo;
int maxAmmo;
}
// Use this for initialization
void Start () {
WeaponSelect.weapon = 1;
WeaponSelect.ammo = 30;
WeaponSelect.maxAmmo = 200;
}
Answer by robertbu · May 21, 2014 at 09:31 PM
You need to make the variables inside the class public:
public struct WeaponSelect {
public int weapon;
public int ammo;
public int maxAmmo;
}
Also, Since the struct is non-static, you won't be able to access the variables inside the struct without creating a reference variable for it.
public WeaponSelect myStruct;
void Start () {
myStruct.weapon = 1;
myStruct.ammo = 30;
myStruct.maxAmmo = 200;
}
Your answer
Follow this Question
Related Questions
How to check if the compiler has an error. 1 Answer
Compiler errors while switching to android 1 Answer
Unity Internal Compiler Error, not there in Editor 0 Answers
Problem with Mono compiler 0 Answers
MySql compiler error help~ (OSX) 0 Answers