Skip to main content
Added the whole code
Source Link

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;
            }
        }
    }
}

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;
            }
        }
    }
}
Source Link

Why isn't OnTriggerExit working?

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!