Search code examples
c#unity-game-enginemodel3dinteraction

Trying to make an object clickable, turn "inactive" and another to turn active. Not sure what is wrong


Well. I am constructing a 3D point n click slice as a project in Unity with C#. It isn't meant to be complicated, and rather I have made it as simple as possible, to try and make it manageable for myself. I am struggling with this script, to try and make an object (That will later be an animated model) interactable. The idea is that you click on the object, said object goes inactive (aka dissapear) while another model appears somewhere else. And then the opposite, when you click on the new object However, the script is not working and I am not 100% sure what I am doing wrong.

My basic script:

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

public class ModelSpawnDespawn : MonoBehaviour
{
    private GameObject SittingGuyc;
    private GameObject StandingGuyc;

    private void OnMouseUpAsButton()
    {
        if(isActiveAndEnabled)
        {
            SittingGuyc.SetActive(false);
            StandingGuyc.SetActive(true);
        }
    }
}

I am very new to programming, so this isn't very complicated and I am sure it is just something basic that I have missed. But I can't seem to find what I did wrong and would love some solid advice from a good community.


Solution

  • I found a solution... The issue was that I had set the game objects to private instead of public. (As well as I needed to change the "Isactiveandenabled" method..

    Current, fully functional script.

        public class ModelSpawnDespawn : MonoBehaviour
    {
        public GameObject NPCPresent;
        public GameObject NPCNotPresent;
        private void OnMouseUpAsButton()
        {
            if (NPCPresent.activeInHierarchy)
            {
                NPCPresent.SetActive(false);
                NPCNotPresent.SetActive(true);
            }
    
        }
    }