Im trying to make it so when you press E on a plank of wood, it will add 1 to the global float Wood, then store this number in another script.
Visual studio reports no errors but unity wont run because it wants a Object reference for the non-static field Egrab.Wood.
Error CS0120: An object reference is required for the non-static field, method, or property 'Egrab.Wood'
I googled it and nothing there seemed to do anything.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Egrab : MonoBehaviour
{
private GameObject Object;
private GameObject Player;
public static float Wood;
void Update()
{
if (Object == null)
{
Object = transform.parent.gameObject;
}
if (Player == null)
{
Player = GameObject.FindGameObjectWithTag("Player");
}
if (Input.GetKey("e"))
{
RaycastHit hit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (Object.name == "wood")
{
Wood = Wood + 1;
Destroy(Object);
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Amount : MonoBehaviour
{
public float amountW;
void Update()
{
amountW = Egrab.Wood;
print(amountW);
}
}
Strange, I copied these classes over and VS throws no such error. Are you sure both files are saved and compiled by Unity? (RMB over any cs file in Project window ->
– andrew-lukasikReimportwill trigger recompilation)