1

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.

5
  • Please dont post code as pictures.. second what is that method supposed to be? Commented Apr 15, 2024 at 23:26
  • I am new here, so I am not sure how else I would post code? And as stated, I am very new to programming and I am not 100% sure what you mean? Commented Apr 15, 2024 at 23:41
  • see tour How to Ask (use code tags) Commented Apr 16, 2024 at 7:02
  • what are SittingGuyc and StandingGuyc? Where do these come from? and is it possible that this component is attached to the one you set to inactive so it is no longer listening? Commented Apr 16, 2024 at 9:20
  • SittingGuyc and StandingGuyc is referencing the two objects that I want to turn active and inactive. Essentially functioning a bit like a switch. When you click on one, that one vanish and the other appear, and if you click on the new one, that one vanish and the other appear. Does that make sense? Commented Apr 16, 2024 at 9:45

1 Answer 1

1

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

    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.