2d issue { 2 objects combine to make a 3rd help}

We’re attempting to make a 2d game involving cross breeding plants, and have run into an issue with combining 2 2d “plants” to create a third new plant. We’re trying to have the 2 plants combine when they enter a box and create a new plant, but nothing happens, no console messages at all. We have tested the code with 3d objects and had them combine and create a new object successfully, so it seems to be a 2d issue. Possibly an issue with our 2d plants as it seems the area we use to combine and create new object works with regular 3d spheres.

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

public class Bine : MonoBehaviour
{
    public List<GameObject> ObjectsToCombine = new List<GameObject>();
    public GameObject resultExample;

    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            //combine("example1", "example2", resultExample, );
        }
    }

    private void OnTriggerEnter2d(Collider other)
    {
        Debug.Log(other.gameObject.name);
        if (!ObjectsToCombine.Contains(other.gameObject))
        {
            ObjectsToCombine.Add(other.gameObject);
        }
        Combine("blue_mushroom_large"/*1*/, "red_toadstool_large"/*2*/, resultExample, other.transform.position);
    }

    void Combine(string plantOne, string plantTwo, GameObject result, Vector3 spawnPosition)
    {
        GameObject plantOneFound/*1*/ = null;
        GameObject plantTwoFound/*2*/ = null;
        for (int i = 0; i < ObjectsToCombine.Count;i++)
        {
            Debug.Log(i);
            if (ObjectsToCombine[i].name == plantTwo)// we could chekc if the current object name contains that string instead
            {
                Debug.Log("plant has entered");
                plantTwoFound/*2*/ = ObjectsToCombine[i];
            }
            if (ObjectsToCombine[i].name == plantOne)// we could chekc if the current object name contains that string instead
            {
                plantOneFound/*1*/ = ObjectsToCombine[i];
            }
        }

        if (plantOneFound/*1*/ && plantTwoFound/*2*/)
        {
            Debug.Log("combine!");
            Destroy(plantOneFound/*1*/);
            Destroy(plantTwoFound/*2*/);
            Instantiate(result, spawnPosition, Quaternion.identity);
            ObjectsToCombine.Clear();//find better way to do this
        }
    }
}

OnTriggerEnter2D

yours say’s

OnTriggerEnter2d

I’m just assuming it’s a syntax error and unity is compiling it as an OnTriggerEnter (which is 3D syntax) and ignoring your 2d creating a semantic error.

hey, this gives me a error, gives me| OnTriggerEnter2D This massage parameter has to be of type Collider2D | any advice, tried doing its fix and broke more. Also never gave me a error of any kind before.

When it says the type has to be Collider2D it means the type has to be Collider2D (not Collider). :wink:

All 2D colliders are derived from Collider2D whereas 3D colliders are derived from Collider.

The docs are your friend: Unity - Scripting API: MonoBehaviour.OnTriggerEnter2D(Collider2D)

1 Like

The Docs are not your friend at all when learning. He may have looked up Collision on Google and seen the Docs about OnTriggerEnter() and wouldn’t know the difference between 2D and 3D functionality. In addition, the Docs are rather vague and almost unintelligible to new programmers. Docs do not become your friend until you have an understanding of the language.

Also, I have a question that I’m too OCD to test. is: OnTriggerEnter2d() the proper syntax?
Or does it need to be: OnTriggerEnter2D()

Will it work either way?

I was referring (and linking) to the API docs for this method in terms of finding the syntax etc. Docs are not good as the only source of information, especially API docs when learning how to use a system, that’s what step-by-step tutorials etc are for but I never said that was the case. I only said they’re your friend, not your only friend and shouldn’t be discounted by beginners. :wink: You seem to be saying they’re not useful at all which I don’t agree with.

The link I provided contains an example of its use and even if you don’t understand it all, it clearly shows the argument types etc:

    // when the GameObjects collider arrange for this GameObject to travel to the left of the screen
    void OnTriggerEnter2D(Collider2D col)
    {
         ....
    }

So I stand by my statement in that they are your friend but not your only friend. :slight_smile: That’s what other devs and tutorials are for. The use of a separate 2D and 3D callback is shown in the manual here but again still cannot replace dedicated tutorials.

This is a C# language question (not Unity) asking if the C# language is case sensitive which it is like many other languages. The above methods are different methods and as you could easily test in a MonoBehaviour, would happily live side-by-side. Unity calls the one defined in the API docs.

THe syntax is perfectly proper. There’s nothing wrong in C# with writing a function called OnTriggerEnter2d. That’s a perfectly valid function.

When Unity has a 2D trigger collision, however, it’s going to look for a function called OnTriggerEnter2D and call that if it exists. If I write a function called OnTriggerEnter2d instead, than it won’t be called. That doesn’t mean there’s any syntax error, though.

This is incorrect, by the way, There is no syntax error and Unity is compiling it as OnTriggerEnter2d, which is not OnTriggerEnter or OnTriggerEnter2D, so this function is simply not going to get called in any Trigger event.

When you write a monobehaviour in Unity, you can put whatever methods in it that you want. There are certain special names (like OnTriggerEnter2D or Update) that Unity will invoke during certain events, but it is perfectly valid to create methods with other names. In that case, Unity will not automatically call them, but you can still call them yourself.

Here is a list of the special names on this page (see where it says messages):
https://docs.unity3d.com/ScriptReference/MonoBehaviour.html

thank you ur amazing and right!

that was part of it, sorry for late reply i got it working after a little of messing with

1 Like

I am glad to help, amigo :slight_smile: