1
\$\begingroup\$

What I want to happen is when the player presses alt, some time passes and an object is instantiated there. I also want it so that the player can't instantiate another object there until they've collecting the one they've planted there already. This is working when I only have one planting area, but once I have more than one planting area it no longer works- for some reason I'm only able to keep planting and collecting in just one of the trigger areas instead of all of them. What am I doing wrong?

public bool instantiated;
public bool inside;
public bool collected;
private IEnumerator coroutine;
public GameObject powerorb;
public Material[] material;
Renderer rend;

void OnTriggerStay2D(Collider2D Collider2D)
{
    if (Collider2D.gameObject.tag == "Player")
    {
        if (instantiated == false)
        {
            inside = true;
        }

        if (collected == true)
        {
            instantiated = false;
            collected = false;
            rend.sharedMaterial = material[0];
        }
    }
}

void OnTriggerExit2D(Collider2D Collider2D)
{
    inside = false;
}


    // Use this for initialization
void Start () {

    rend = GetComponent<Renderer>();
    rend.enabled = true;
}

// Update is called once per frame
void Update () {  

    if (inside && Input.GetKeyDown(KeyCode.LeftAlt))
    {
        StartCoroutine("spawning");
    }


    if (instantiated == true)
    {
        StopCoroutine("spawning");        
    }
}

IEnumerator spawning()
{
    rend.sharedMaterial = material[1];

    yield return new WaitForSeconds(8);
    Instantiate(powerorb, transform.position, transform.rotation);
    instantiated = true;
    inside = false;

    yield return null;

}

I reference if the object has been collected in another script with this:

 if (collecting)
        {
            GameObject.FindWithTag("PowerFarm").GetComponent<PowerFarmCoroutine>().collected =
        true;
            Destroy(gameObject);
        }
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

Look at this code:

void OnTriggerStay2D(Collider2D Collider2D)

The variable named the same as the type could be causing some issues.

\$\endgroup\$
5
  • \$\begingroup\$ Definitely a big red flag but the parameter name shouldn't be the cause of his problem, unless we aren't seeing the same thing \$\endgroup\$ Commented Feb 17, 2019 at 6:42
  • \$\begingroup\$ No I don't see it? I tried moving the if (collected == true) part out of ontriggerstay and into update instead but that didn't seem to help? \$\endgroup\$
    – poprocks
    Commented Feb 17, 2019 at 7:06
  • \$\begingroup\$ @CedricMartens You’re right. It probably isn’t the cause of his problem, but I had to point it out. \$\endgroup\$
    – Sean Carey
    Commented Feb 17, 2019 at 7:31
  • \$\begingroup\$ @poprocks it is considered bad practice to put your argument name the exact name than the type. You should not have Collision2D twice with a capital C. The first Collision2D is the type so that's fine but the second should start with a small c. This won't fix your problem. It's just better programming practice \$\endgroup\$ Commented Feb 17, 2019 at 13:36
  • \$\begingroup\$ Oh thanks for explaining further, I didn't know that! \$\endgroup\$
    – poprocks
    Commented Feb 17, 2019 at 18:00
0
\$\begingroup\$

I actually just found a solution but it's pretty messy and bootleg. What I did was I turned off the mesh renderer and translated the object really fast to trigger ontriggerexit, then destroyed it. I'm a little worried it may cause me problems later because of the messiness so I'd like to find another solution but here's what I have so far:

public bool instantiated; 
public bool inside;
private IEnumerator coroutine;
public GameObject powerorb;
public Material[] material;
Renderer rend;

void OnTriggerStay2D(Collider2D Collider2D)
{
    if (Collider2D.gameObject.tag == "Player")
    {
        if (instantiated == false)
        {
            inside = true;
        }
    }        
}

void OnTriggerExit2D(Collider2D Collider2D)
{
    if (Collider2D.gameObject.tag == "Player")
    {
        inside = false;
    }

    if (Collider2D.gameObject.tag == "FarmOrb")
    {
       instantiated = false;
       rend.sharedMaterial = material[0];
    }
}


    // Use this for initialization
void Start () {

    rend = GetComponent<Renderer>();
    rend.enabled = true;
}

// Update is called once per frame
void Update () {    

    if (inside && Input.GetKeyDown(KeyCode.LeftAlt))
    {
        StartCoroutine("spawning");
    }


    if (instantiated == true)
    {
        StopCoroutine("spawning");          
    }
}

IEnumerator spawning()
{
    rend.sharedMaterial = material[1];

    yield return new WaitForSeconds(8);
    Instantiate(powerorb, transform.position, transform.rotation);
    instantiated = true;
    inside = false;

    yield return null;

}

And then in the script where the object is being collected:

           if (collecting)
        {

            transform.Translate(Vector3.up * 5 );
            gameObject.GetComponent<Renderer>().enabled = false;            
            startDisappearCount = true;
        }

    if (startDisappearCount == true)
    {
        disappearCount -= Time.deltaTime;

        if (disappearCount <= 0)
        {
            Destroy(gameObject);
        }
    }
\$\endgroup\$

You must log in to answer this question.

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