0
\$\begingroup\$

I have a price of code that looks like this:

void OnTriggerExit() {
 Debug.Log("Object leaved trigger");
}

But when I move an object that is colliding with the trigger by script it doesn't print to the console, but if i nudge it instead it does. I'm guessing it's because the object ain't actually exiting, but disappearing. Is there a way to solve this? Thank you!

EDIT:
Okey, so this is the actual code:

Craftingstation.cs

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

public class Craftingstation : MonoBehaviour {

    //GameObject globalScripts;

    bool itemAlreadyCraftable;
    int correctItems;

    public string[,] itemIds;
    // Generic list for holding items on crafter
    public List<string> itemsOnCrafter;
    public List<string> craftableItems;
    // Static 2D array for holding crafting recipes
    string[,] craftingRecipes = new string[,] {
        // {recipe},{result}
        {"potato","tabasco"},{"explosivePotato"},
        {"mentos","cola"},{"colaCannon"},
        {"potato","cola"},{"potatoCola"},
        {"potato","chips"},{"potatoChips"}
    };

    void Start () {
        //globalScripts = GameObject.FindGameObjectWithTag("Global Scripts");
        //string test = globalScripts.GetComponent<items>().nameToId("mentos");
    }

    /*
    void nameToId(string passedName) {
        for (int i = 0; i < globalScripts.GetComponent<items>().itemIds.GetLength(0); i++)
        {
            string loopedName = globalScripts.GetComponent<items>().itemIds[i, 0];
            if (loopedName == passedName) {
                string returnString = globalScripts.GetComponent<items>().itemIds[i, 1];
                Debug.Log(returnString);
            }
        }
        return 0;
    }
    */

    void Update () {
        for (int i = 0; i < craftingRecipes.GetLength(0); i++) {
            itemAlreadyCraftable = false;
            correctItems = 0;
            if (i%2==0) {
                for (int j = 0; j < craftingRecipes.GetLength(1); j++) {
                    for (int k = 0; k < itemsOnCrafter.Count; k++) {
                        if (itemsOnCrafter[k] == craftingRecipes[i, j]) {
                            correctItems++;
                        }
                    }
                    if (correctItems >= craftingRecipes.GetLength(1)) {
                        for (int l = 0; l < craftableItems.Count; l++) {
                            if (craftableItems[l] == craftingRecipes[i+1, 0]) {
                                itemAlreadyCraftable = true;
                            }
                        }
                        if (itemAlreadyCraftable == false) {
                            craftableItems.Add(craftingRecipes[i+1, 0]);
                        }
                    }
                    else {
                        craftableItems.Remove(craftingRecipes[i+1, 0]);
                    }
                }
            }
        }
    }

    void OnTriggerEnter(Collider other) {
        itemsOnCrafter.Add(other.GetComponent<id>().technicalName);
    }
    void OnTriggerExit(Collider other) {
        itemsOnCrafter.Remove(other.GetComponent<id>().technicalName);
    }
}

sendRay.cs

using UnityEngine;
using System.Collections;

public class sendRay : MonoBehaviour {

    Ray ray;
    RaycastHit hit;
    bool holding = false;
    public bool pickedUp = false;
    Vector3 tempScale;


    void Update()
    {
        if(Input.GetButtonDown("Pickup"))
        {
            Vector2 screenCenterPoint = new Vector2(Screen.width/2, Screen.height/2);
            ray = Camera.main.ScreenPointToRay(screenCenterPoint);

            if(Physics.Raycast(ray, out hit, 5))
            {
                if(hit.transform.tag == "Pickup") {
                    if(holding == false) {
                        pickedUp = true;
                        tempScale = hit.transform.localScale;
                        hit.transform.parent = GameObject.FindGameObjectWithTag("Hand").transform;
                        float distX = hit.transform.GetComponent<keepOffset>().distX;
                        float distY = hit.transform.GetComponent<keepOffset>().distY;
                        float distZ = hit.transform.GetComponent<keepOffset>().distX;
                        //Debug.Log(distX + ", " + distY + ", " + distZ);
                        hit.transform.localPosition = new Vector3(-distZ, -distX, -distY);
                        hit.transform.localScale = tempScale;
                        hit.transform.rotation = GameObject.FindGameObjectWithTag("Hand").transform.rotation;
                        foreach(Collider c in hit.transform.GetComponents<Collider>()) {
                            c.enabled = false;
                        }
                        foreach(Rigidbody r in hit.transform.GetComponents<Rigidbody>()) {
                            r.isKinematic = true;
                            r.detectCollisions = false;
                        }
                        holding = true;
                        return;
                    }
                }
            }
            if(holding == true) {
                pickedUp = false;
                foreach (Transform child in GameObject.FindGameObjectWithTag("Hand").transform){
                    child.transform.parent = null;
                    child.transform.localScale = tempScale;
                    foreach(Collider c in child.transform.GetComponents<Collider>()) {
                        c.enabled = true;
                    }
                    foreach(Rigidbody r in child.transform.GetComponents<Rigidbody>()) {
                        r.isKinematic = false;
                        r.detectCollisions = true;
                    }
                }
                holding = false;
                return;
            }
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Can you show the code you use to move the object? \$\endgroup\$ – Jeremiah Leslie Jun 18 '15 at 23:22
  • \$\begingroup\$ Are you working with 2D? \$\endgroup\$ – SanSolo Jun 19 '15 at 4:39
  • \$\begingroup\$ Also what do you mean by object is disappearing? \$\endgroup\$ – SanSolo Jun 19 '15 at 4:41
1
\$\begingroup\$

Have a look at the online API.

It states that the function requires an argument of type Collider, which is what you're missing.

It should be:

void OnTriggerExit(Collider other)

Not:

void OnTriggerExit()

Another note, make sure one of the objects colliding have a rigidbody, Unity requires it to detect collisions.

\$\endgroup\$
  • 2
    \$\begingroup\$ It will work without the Collider parameter. The parameter is required only if you want to do something to the object that this object collided with. e.g: Destroy(other.gameObject); \$\endgroup\$ – SanSolo Jun 19 '15 at 4:39
  • \$\begingroup\$ @SanSolo Oh really? I honestly had no clue, my apologies. The question is lacking some detail for it to be answered decisively. \$\endgroup\$ – Anthony Ngo Jun 19 '15 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.