0
\$\begingroup\$

Befor Star: (It's basic cube)

enter image description here

After Start: (It's must be Eye)

enter image description here

Code:

IngredientAsset:

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

[CreateAssetMenu(fileName = "New Ingredient", menuName = "Ingredient")]
public class IngredientAsset : ScriptableObject {

    public Mesh ingredientMesh;

}

Ingredient:

using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Ingredient : MonoBehaviour {

    public IngredientAsset asset;



    public Ingredient (IngredientAsset a) {
        asset = a;
    }

    public bool HasIngrediend()
    {
        if (asset == null) {
            return false;
        } else {
            return true;
        }
    }

    public Mesh GetIngrediendMesh()
    {
        if (asset == null) {
            return null;
        }

        return asset.ingredientMesh;

    }


}

TableBox:

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

public class TableBox : MonoBehaviour {

    public MeshFilter overlay;

    public IngredientAsset ingredient;
    public EquipmentAsset equipment;

    private void Start()
    {
        if (ingredient!=null) {
            overlay.mesh = ingredient.ingredientMesh;
        } else if (equipment != null) {
            overlay.mesh = equipment.equipmentMesh;
        }
    }

    public void Interact(IngredientAsset c, EquipmentAsset t, PlayerInteraction player)
    {
        player.SetIngredient(ingredient);
        ingredient = c;
        player.SetEquipment(equipment);
        equipment = t;

        if (equipment != null)
            overlay.mesh = equipment.equipmentMesh;
        else if (ingredient != null)
            overlay.mesh = ingredient.ingredientMesh;
        else
            overlay.mesh = null;
    }
}

PlayerInteraction:

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

public class PlayerInteraction : MonoBehaviour {

    public GameObject target;

    public KeyCode interactKey;


    [SerializeField]
    private IngredientAsset ingredient;
    [SerializeField]
    private EquipmentAsset equipment;

    private void Update()
    {
        if (Input.GetKeyDown(interactKey))
        {
            if (target == null)
                return;

            TableBox table = target.GetComponent<TableBox>();
            if (table != null)
            {
                table.Interact(ingredient, equipment, this);
            }

        }
    }

    public void SetIngredient(IngredientAsset c)
    {
        ingredient = c;

    }


    public void SetEquipment(EquipmentAsset t)
    {
        equipment = t;

    }


    private void OnTriggerEnter(Collider col)
    {
        if (target != col.gameObject && target != null)
        {
            Deselect();
        }

        target = col.gameObject;

        }
    }

    private void OnTriggerExit(Collider col)
    {
        if (col.gameObject == target)
        {
            Deselect();
            target = null;
        }
    }

    void Deselect()
    {

    }
}

My project.

P.S. What if I wanna change my code to work with prefabs, what I must to do? Or What I must to do that my mesh looks like my prefabs?

Thanks for attention!

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.