Hey,
So what i try to reach is that i can change the amount of a item i have.
For example: I see a wood and wen i pick it it up it wil go to the “ItemSystem” Script and say hey this is this type of item (Wood) And add 1 by the amount of the list. but what is the best way to do it or do at all.
All help is Appreciated!
ItemSystem Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ItemSystem : MonoBehaviour {
public List<Items> itemobjects = new List<Items>();
}
[System.Serializable]
public class Items
{
public string Name;
public int Amount;
public Text Amount_T;
}
GameObjectInfo Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gameObjectInfo : MonoBehaviour {
public string InfoText = "None Added yet"; //For Example: To Pick up, To Interact
public KeyCode InteractKey = KeyCode.None;
public Color Textcolor;
public Color Outlinecolor;
public ItemSort Item;
GameObject ThisObjec;
private string CompleteText;
GameObject Manager;
void Awake()
{
Manager = GameObject.FindWithTag("Manager");
ThisObjec = gameObject;
}
public void Dothing()
{
if(Item == ItemSort.Item)
{
Manager.GetComponent<ItemSystem>(); // Add here the code for chnging the amount
Debug.Log("Collect"+gameObject);
}
}
public void GiveData()
{
CompleteText = "["+InteractKey +"]" +" " +InfoText;
Cam.GetComponent<Interact>().RecieveData(CompleteText, InteractKey, Textcolor, Outlinecolor);
}
}
public enum ItemSort
{
None,
Material,
Item
}
Interact Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Interact : MonoBehaviour {
public float InteractDistance = 5;
public Text Text;
public GameObject InteractInfo;
private string inter_Info;
private KeyCode inter_Key = KeyCode.None;
public GameObject LastHit;
void Update()
{
//-----------------------------------------------------------------------------
Vector3 forward = transform.TransformDirection(Vector3.forward) * InteractDistance;
Debug.DrawRay(transform.position, forward, Color.red);
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, InteractDistance))
{
if (hit.collider.gameObject.GetComponent<gameObjectInfo>() != null)
{
LastHit = hit.collider.gameObject;
hit.collider.gameObject.GetComponent<gameObjectInfo>().GiveData();
hit.collider.gameObject.GetComponent<Outline>().enabled = true;
InteractInfo.SetActive(true);
if (Input.GetKeyDown(inter_Key))
hit.collider.gameObject.GetComponent<gameObjectInfo>().Dothing();
}
else
{
LastHit.GetComponent<Outline>().enabled = false;
InteractInfo.SetActive(false);
}
}
else
{
LastHit.GetComponent<Outline>().enabled = false;
InteractInfo.SetActive(false);
}
}
public void RecieveData(string InfoText, KeyCode Interkey, Color textColor, Color OutlineColor)
{
inter_Info = InfoText;
inter_Key = Interkey;
Text.color = textColor;
//InteractInfo.GetComponent<Outline>().effectColor = OutlineColor;
Text.text = inter_Info.ToString();
}
}