- Home /
Enum Type Inventory?
I have an "if" statement: if(curBlock = BrickType.Dirt) and I want it to see is your "Current Block" is dirt. BrickType is an enum: public enum BrickType {Dirt, ect...}, because I have many different block types. So, I want it to be so that when I break a block, it checks what type of block it is, then it adds it to your inventory, but I get the error: Assets/Scripts/PlayerIO.cs(69,31): error CS0119: Expression denotes a
type', where a variable',
value' or `method group' was expected. So is there a way I can have it check what block it is? Also is there a easier way to add it to your inventory, instead of having an "if" statement for every block?
Answer by robertbu · Dec 24, 2013 at 03:37 AM
Your problem is that comparisons use two equal signs ('=='), so your line should be:
if(curBlock == BrickType.Dirt)
A switch statement is bit of an improvement:
switch (curBlock) {
case BrickType.Dirt:
// Do the dirt stuff
break;
case BrickType.Plastic:
// Do the plastic stuff
break;
}
There may be other improvements, but I'd have to understand more about your code...in particular, why you need to know what type when adding it to the inventory.
Thanks robertbu, but now im getting this error: Assets/Scripts/PlayerIO.cs(69,20): error CS0019: Operator ==' cannot be applied to operands of type
byte' and `BrickType'
I need to see more of your script. In particular I need to see how you've declared 'curBlock'. For the comparison to work, 'curBlock' has to be of 'BrickType'. Is this C# or Javascript?
Here is my entire code for breaking and placing blocks:
using UnityEngine;
using System.Collections;
public class PlayerIO : $$anonymous$$onoBehaviour {
public float RStone,SStone,DStone,PStone,Gr,Di,Sa,GrL,Ta,Du,Ru,Ic,Sn,DIce,St,La;
public float maxBrick = 10;
public static PlayerIO currentPlayerIO;
public float maxInteractionRange = 8;
public byte curBlock = 0;
// Use this for initialization
void Start () {
Screen.lockCursor = true;
currentPlayerIO = this;
}
// Update is called once per frame
void Update () {
Ray ray = camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.5f));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, maxInteractionRange))
{
Chunk chunk = hit.transform.GetComponent<Chunk>();
if (chunk == null)
{
Debug.Log("Clicked on " + hit.transform.name + " and it's not a chunk.");
return;
}
Vector3 p = hit.point;
p.y /= World.currentWorld.brickHeight;
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
{
p -= hit.normal / 4;
curBlock = chunk.GetByte(p);
Debug.Log("Picked up " + (BrickType)curBlock);
chunk.SetBrick(0, p);
Inventory();
}
else
{
if(curBlock != 0)
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse1))
{
p += hit.normal / 4;
chunk.SetBrick(curBlock, p);
Debug.Log("Put down " + (BrickType)curBlock);
curBlock = 0;
}
}
}
}
else
{
Debug.Log("Clicked, but nothing was there!");
}
}
void Inventory()
{
//if(curBlock == BrickType.Dirt)
//{
//Di ++;
//OnGUI();
//}
}
}
You just need to cast it like you've done above:
if((BrickType)curBlock == BrickType.Dirt)
But if it were me, I'd do the other way around. I declare curBlock as:
public BrickType curBlock; // Does it need to be public?
Then I'd do the assignment as:
curBlock = (BrickType)chunk.GetByte(p);
Then you would not need the cast in the if() statement.
O$$anonymous$$G(<--I hate using that) Thank you so much! It works!
Your answer
![](https://koobas.hobune.stream/wayback/20220613124757im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
BCE0051 Error 1 Answer
apply something on enum 1 Answer
if statement error 1 Answer
C# Enum for Animation Issue 1 Answer