0
\$\begingroup\$

I've been having issues with a game I've been working on for a while. It's a pretty big project. I'm having some issues with two tree prefab game objects sharing code, where I don't want them to.

When the player presses E while facing them, they start searching the tree for items like apples. It was all working well, until I added another tree. Now, when I search one tree, it searches all trees in the scene. Obviously, I don't want that to happen.

I have provided my code, below. What am I doing wrong?

public class Search : MonoBehaviour 
{
    public List<Item> items;
    public bool searched = false;
    public GameObject inventory;
    public Inventory inv;
    public Log log;
    public AudioSource takeSFX;
    public GameObject player;

    void Awake()
    {
        inventory = GameObject.Find("  Inventory Master");
        inv = inventory.GetComponent<Inventory>();
    }

    void Start()
    {
        log = GameObject.Find("  Game Master").GetComponent<Log>();
        AddItems();
        items = new List<Item>();
    }

    void Update()
    {
        SearchObject();
    }

    public void AddItems()
    {
        int rand = (int)Random.Range(0, 2);
        Debug.Log(rand);

        for (int i = 0; i < rand; i++)
        {
            items.Add(inv.database.FetchItemByID(0));
        }
    }

    public void SearchObject()
    { 
        if (Input.GetButtonDown("Interact"))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit = new RaycastHit();
            Debug.Log("Raycasting");

            if (Physics.Raycast(ray, out hit, 10))
            {
                Debug.Log("Searching");

                if (!searched)
                {
                    this.takeSFX.Play();

                    if (items.Count >= -0)
                    {
                        for (int i = 0; i < items.Count; i++)
                        {
                            this.inv.AddItem(items[i].ID);
                        }
                    }    

                    if (items.Count == 0)
                        log.UpdateLog("No items found in search.");
                    else if (items.Count == 1)
                        log.UpdateLog("1 item found in search.");
                    else
                        log.UpdateLog(items.Count + " items found in search.");
                    searched = true;
                }
            }
        }
    }
}    
\$\endgroup\$
1
  • \$\begingroup\$ Using this script, every tree fires a ray, checks whether that ray hit something (not necessarily the tree that's currently running this script, not necessarily a tree at all - you never check what the ray hit) then sets its searched value to true. You probably want the player character to fire the ray (so it happens only once - raycasts have some cost) and then search/interact with the first thing that the ray hit. That way you can ensure you're only ever searching one thing at a time. \$\endgroup\$
    – DMGregory
    Commented Nov 3, 2016 at 22:07

1 Answer 1

2
\$\begingroup\$

Your raycast is being cast for every tree in the scene. Every tree is checking if the raycast hit something, even if it's not that tree.

This is happening because you are getting the input event inside the tree object. You should centralize this Event Handler, adding it to inside your PlayerInput.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .