[Solved] How to change things a list from another script?! C#

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();
    }
}

You haven’t associated the item that you collided with to an actual item yet. Your GameObjectInfo class needs to have an item associated with it or at least it’s name/id.

From what you have now the easiest way and least code changes would be to add a ‘public string name’ variable to GameObjectInfo. You will have to set this sometime. Then in Dothing:

public void Dothing()
{
    if(Item == ItemSort.Item)
    {
        ItemSystem itemSystem = Manager.GetComponent<ItemSystem>(); // Add here the code for chnging the amount
        for (int i = 0; i < itemSystem.itemobjects.Count; i++) {
            if (itemSystem.itemobjects[i].name == name) {
                itemSystem.itemobjects[i].Amount++;
            }
        }
        Debug.Log("Collect"+gameObject);
    }
}

While this isn’t the best way, it’s the least intrusive with your current code.
I’d also likely move the Manager.GetComponent() call to the void Awake method as this function isn’t good to call over and over.

1 Like

Hey, juicyz
This was where was looking for thank you so much!
And it works perfect!!!