Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

I want to destroy gameObject in outter screen, Help.

Discussion in 'Scripting' started by WENKO, Aug 18, 2021.

  1. WENKO

    WENKO

    Joined:
    Apr 27, 2019
    Posts:
    39
    1. I want to erase the gameObjects automatically in outter Screen.
    OnScreen.jpg
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Detection : MonoBehaviour
    6. {
    7.     public Camera mainCamera;
    8.    
    9.     void Awake()
    10.     {
    11.       mainCamera = GetComponent<Camera>();
    12.     }
    13.     void Update()
    14.     {
    15.         Vector2 position = new Vector2();
    16.         Vector2 screenPosition = mainCamera.WorldToScreenPoint(transform.position);
    17.  
    18.         float screenHalfHeight = Camera.main.orthographicSize;
    19.         float screenHalfWidth = screenHalfHeight * Camera.main.aspect;
    20.  
    21.         position.x = Random.Range(-screenHalfWidth, screenHalfWidth);
    22.         position.y = Random.Range(-screenHalfHeight, screenHalfHeight);
    23.  
    24.         if (screenPosition.x != position.x || screenPosition.y != position.y)
    25.         {
    26.             Destroy(this.gameObject);
    27.         }
    28.     }
    29. }
    2. but Error is there

    111.JPG

    NullReferenceException: Object reference not set to an instance of an object
    Detection.Update () (at Assets/Scripts/Detection.cs:16)

    222.JPG

    3. So, How could I drag the Camera to the Inspector??
    Help me
     
  2. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    Don't know why you can't drag your camera in the inspector but even if you could, it won't work since you set your mainCamera to null in your Awake function.
     
  3. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    You cannot drag a GameObject into a component. You can just reference in start without a public variable if it will always be the main camera.

    Code (CSharp):
    1. mainCamera = Camera.main
     
    Last edited: Aug 19, 2021
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Technically you can drag components all day long. Try it: put two BoxColliders on something, then pick each one (via the inspector) and you can drag it into a slot, either on the same GameObject, or by opening TWO inspector windows, or by clever locking of your inspector window.

    This is indeed, as Adehm points out, wiping out anything you dragged in.

    This if statement is ALWAYS going to fire. Floating point numbers, especially ones returned from Random.Range(), are essentially NEVER going to be equal.

    Do NOT test floating point numbers for equality, except in extremely contrived cases such as assignment from a constant, but even then you're playing with fire.

    Instead, ask if they are close enough by taking the Mathf.Abs() of their difference.