#Object is not setting active

1 messages · Page 1 of 1 (latest)

stoic glacier
#

I have this script that detects when the orb is touched and destroys it after activating the camera on the thing it touched it.Now the problem is that the camera isn't activating even tho it should, why?

using System;
using UnityEngine;

public class OrbTouched : MonoBehaviour
{

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "Human")
        {
            Transform[] TableArray = GetComponentsInChildren<Transform>();

            for (int i = 0;i < TableArray.Length; i++)
            {
                if (TableArray[i].gameObject.tag == "MainCamera")
                {
                    TableArray[i].gameObject.SetActive(true);


                    //IT DESTROYS AFTER ITS DONE
                    Destroy(gameObject);
                    
                }
            }
            
        }
    }
}
smoky coral
#

This is a bit confusing.

  1. Which obejct is this script on? The orb the player is touching?
  2. Which object has the camera? The orb, or the Human it's touching

Because the way you wrote your code:

  • The object this script is on is expecting to touch a human
  • If it does, it is searching ITS OWN children for a camera.
  • When it finds a camera it activates the camera but then it destroys itself and therefore the camera too
#

Did you mean to do:

Transform[] TableArray = collision.gameObject.GetComponentsInChildren<Transform>();

Instead of:

Transform[] TableArray = GetComponentsInChildren<Transform>();```?
stoic glacier
#

Ok that makes sense

#

Fixed

#

Thanks

smoky coral
stoic glacier