Hi guys, I’m sort of new to scripting but I’ve followed several guides and instructions to the letter and cannot figure out why this isn’t working -
The basic idea of the script is that when the player clicks on something it will check what the object is - if it is a chest then I want to check that objects variables to see a few things about that chest, starting with if it has been looted already, however get component is not working.
here are all the relevant lines of code, please help!
public class Interact : MonoBehaviour {
private Vector3 playerLoc;
private Vector3 interactLoc;
public GameObject player;
private GameObject interactedObject;
void Update ()
{
if (Input.GetMouseButtonDown (0))
{
GetInteraction ();
}
}
void GetInteraction()
{
Ray interactionRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit interactionInfo;
if (Physics.Raycast (interactionRay, out interactionInfo, Mathf.Infinity)) {
interactedObject = interactionInfo.collider.gameObject;
playerLoc = player.transform.position;
interactLoc = interactedObject.transform.position;
Debug.Log (“you touch” + interactedObject);
// Check if object is a chest
if (interactedObject.tag == “Chest”)
{
float dist = Vector3.Distance (playerLoc, interactLoc);
Debug.Log (dist);
if (dist >= 1.8f)
{
Debug.Log (“You need to get closer to open that.”);
}
else
{
ChestAction ();
}
//Interact with Chest script
public virtual void ChestAction(){
Debug.Log (“You are close enough to the chest.”);
bool lootable = interactedObject.GetComponent.chestLooted;
if (lootable == (false)) {
}
}
And here is the code from the ‘InteractChest’ script that is attached to the chest object that should be getting interacted with.
public class InteractChest : MonoBehaviour {
public int chestGold;
public bool chestLocked = (false);
public bool chestLooted = (false);
public string chestItems = (“None.”);
void Awake(){
chestGold = Random.Range(5, 32);
}
}
No matter where I search, I can’t find a way on how to get the information from chestLooted that is attached to whatever chest is being interacted with, what am I doing wrong? ![]()