On click resource collection

So, I’m going a little off Rick’s setup for collecting resources, and I’ve run into a bit of a wall. What I’m going for is something more similar to Plants Vs. Zombies where the unit spawns a resource that you have to click to collect. I’ve pasted the code I have so far below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spore : MonoBehaviour
{
    [SerializeField] GameObject collectParticles;
    [SerializeField] AudioClip collectSFX;
    [SerializeField] float collectSFXDuration = 5f;

    public void OnMouseDown()
    {
        AddSpores();
        TriggerCollectionVFX();
        Destroy(gameObject);
    }

    private void TriggerCollectionVFX()
    {
        GameObject collectVFX = Instantiate(collectParticles, transform.position, transform.rotation);
        Destroy(collectVFX, collectSFXDuration);
    }

    public void AddSpores(int amount)
    {
        FindObjectOfType<SporeDisplay>().AddSpores(amount);
    }
}

The problem I’m having is I want OnMouseDown() to call AddSpores(int amount), but I’m getting errors about the arguments not matching up. I tried plugging them in in various ways but nothing seems to work. Help would be greatly appreciated!

Hi Bembrooks,

Since AddSpores is defined with a parameter, you need to pass on an int value when calling AddSpores. At the moment, the parentheses in the method call are empty.

I figured it was something like that, but I think I’m not understanding something still. I’ve tried to pass int, int amount, and amount into that method call and none of them work.

Ok, actually I think I figured out a solution. I just serialized an int field called amount and passed that info the addspores call.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spore : MonoBehaviour
{
    [SerializeField] GameObject collectParticles;
    [SerializeField] AudioClip collectSFX;
    [SerializeField] float collectSFXDuration = 5f;
    [SerializeField] int amount = 50;
    
    private void OnMouseDown()
    {
        AddSpores(amount);
        TriggerCollectionVFX();
        Destroy(gameObject);
    }

    private void TriggerCollectionVFX()
    {
        Debug.Log ("the particle should be here");
        GameObject collectVFX = Instantiate(collectParticles, transform.position, transform.rotation);
        Destroy(collectVFX, collectSFXDuration);
    }

    public void AddSpores(int amount)
    {
        FindObjectOfType<SporeDisplay>().AddSpores(amount);
    }
}

Although I am curious why it wouldn’t let me add the int value it was getting from the SporeDisplay script I as accessing at the bottom of this script.

Good job on fixing the problem! :slight_smile:

The Spore instance does not know anything about the SporeDisplay instance or any other instance. For this reason, the amount variable of other instances is unknown to it.

The code is not getting any value from the SporeDisplay instance but a reference (“link”) to a SporeDisplay instance. And then it calls the AddSpores method of that instance, given FindObjectOfType found an instance.

Don’t get confused by names. Variable names are not global. amount declared in in ClassA is a completely different variable than amount declared in ClassB.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms