0

I'm trying to set a game object active in my Unity 3D project (with Oculus SDK).

Hopefully this would be set active when the player collides with triggerCube (specifically want to be able to pick up triggerCube and then ViewSight gameobject is set active.

When player lets go, ViewSight gameobject is set to false.

I've tried turning off ViewSight gameobject in the inspector but still having it in the hierarchy. It didn't work so I made a prefab of it and deleted it from the hierarchy (and pulled it in to the script in the inspector for the triggerCube) and it didn't work that way either. Not sure what I'm doing wrong.

Thanks for your help.

This is the script I have and its attached to the triggerCube:

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

public class ViewFinderStateTrigger : MonoBehaviour
{

    public GameObject ViewSight;

    public void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.name == "triggerCube")
        {
            ViewSight.SetActive(true);
            Debug.Log("Active");

        }
        else
        {
            ViewSight.SetActive(false);
            Debug.Log("Not Active");
        }
    }
}
zero323
  • 322,348
  • 103
  • 959
  • 935
Matthew_C
  • 1
  • 2
  • 1
    What exactly isn't working? Is the code not running? – MyiEye Nov 05 '18 at 19:21
  • Sorry just to give you more detail, the code builds with no issues. On the console it shows the debug.log "Not Active" correctly but when I collide it says Instanciating and then shoots the error ArgumentException: The Object you want to instantiate is null. So my object is never set active – Matthew_C Nov 05 '18 at 20:16
  • You need to assign the value. There are literally thousands of duplicates of that error all pointing [here](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Draco18s no longer trusts SE Nov 05 '18 at 20:32
  • Thanks I'll look into that, I thought I had given it a value by naming GameObject and pulling the prefab into into the script in the inspector but I guess I misunderstood. – Matthew_C Nov 05 '18 at 20:55
  • That is generally how that works, yes. But there are reasons that even that would fail (eg if the object is destroyed by another script). – Draco18s no longer trusts SE Nov 05 '18 at 21:15

0 Answers0