in unity i want to destroy a gameobject when it hits the edge of the screen
C#
// this does take into account EVERY camera, including the editor view one
// but that shouldn't be a big deal once the game is built
void OnBecameInvisible() {
Destroy(gameObject);
} using UnityEngine;
using System.Collections;
public class CarDetection : MonoBehaviour {
private Camera mainCamera;
public Vector2 widthThreshold;
public Vector2 heightThreshold;
void Awake () {
//Get your mainCamera here. If you are pretty sure that the camera is always the Camera.main you don't need to implement here. Just call for Camera.main later.
}
void Update () {
Vector2 screenPosition = mainCamera.WorldToScreenPoint (transform.position);
if (screenPosition.x < widthThresold.x || screenPosition.x > widthThresold.y || screenPosition.y < heightThresold.x || screenPosition.y > heightThresold.y) {
Destroy (gameObject);
}
}
}
Also in C#:
- Title
- c# build string out of list of strings
- Category
- C#
- Title
- winforms timer c#
- Category
- C#
- Title
- first sentence letter capital in c#
- Category
- C#
- Title
- exception handling c# stack overflow
- Category
- C#
- Title
- read xml file c#
- Category
- C#
- Title
- count number of enum values C#
- Category
- C#
- Title
- unity onclick object
- Category
- C#
- Title
- c# docstring
- Category
- C#
- Title
- set label position winforms
- Category
- C#
- Title
- foreach c#
- Category
- C#
- Title
- c# else if
- Category
- C#
- Title
- c sharp exit while loop
- Category
- C#
- Title
- sort c#
- Category
- C#
- Title
- c sharp tryparse
- Category
- C#
- Title
- unity instantiate an object
- Category
- C#
- Title
- get random value from list c#
- Category
- C#
- Title
- c# silent execute exe
- Category
- C#
- Title
- linq c# where condition
- Category
- C#
- Title
- Category
- C#
- Title
- change z value unity
- Category
- C#
- Title
- how to make pressing enter an event in c#
- Category
- C#
- Title
- unity jump
- Category
- C#
- Title
- how does Pow work C#
- Category
- C#
- Title
- .net core session
- Category
- C#
- Title
- file picker uwp c#
- Category
- C#
- Title
- unity getcomponent
- Category
- C#
- Title
- c# close window
- Category
- C#
- Title
- httpclient soap request c#
- Category
- C#
- Title
- prefab gets character transform
- Category
- C#
- Title
- c# map
- Category
- C#
- Title
- asp.net render control to string
- Category
- C#
- Title
- unity how to rotate something to point to something else
- Category
- C#
- Title
- unity movetowards 2d
- Category
- C#
- Title
- linq foreach c#
- Category
- C#
- Title
- c# inheritance constructor
- Category
- C#
- Title
- how to get odd saturday in a month in c#
- Category
- C#
- Title
- compose graphql query string in c#
- Category
- C#
- Title
- c# group array based on first character
- Category
- C#
- Title
- c# make request to rest api
- Category
- C#
- Title
- verifyusertokenasync password reset token
- Category
- C#
- Title
- string to enum c# 3
- Category
- C#
- Title
- c# data types
- Category
- C#
- Title
- unity set position
- Category
- C#
- Title
- datagridview select row column cell c#
- Category
- C#
- Title
- c# how to take from a float
- Category
- C#
- Title
- unity add sections to a list
- Category
- C#
- Title
- error CS0542
- Category
- C#
- Title
- c# find index element array
- Category
- C#
- Title
- unity list
- Category
- C#
- Title
- c# split large file into chunks
- Category
- C#
- Title
- odbc command parameters c#
- Category
- C#
- Title
- iframe set html content c#
- Category
- C#
- Title
- c# how to delete a file
- Category
- C#
- Title
- idbset sqlquery
- Category
- C#
- Title
- c# select first value from list
- Category
- C#
- Title
- unity making homing rocket
- Category
- C#
- Title
- how to say or in c#
- Category
- C#
- Title
- asp.netcore: develop on win10 run on ubuntu
- Category
- C#
- Title
- asking for user input integer c#
- Category
- C#
- Title
- c# get char from string
- Category
- C#
- Title
- c# max two values
- Category
- C#
- Title
- asp.net c# write string to text file
- Category
- C#
- Title
- weakreference tryget c#
- Category
- C#
- Title
- c# long to int
- Category
- C#
- Title
- c# distinct comparer multiple properties
- Category
- C#
- Title
- Globalization and localization in ASP.NET Core
- Category
- C#
- Title
- Celsius to Fahrenheit c#
- Category
- C#
- Title
- textblock line break
- Category
- C#
- Title
- strtorime online
- Category
- C#
- Title
- unity change tag in script
- Category
- C#
- Title
- convert string to datetime c#
- Category
- C#
- Title
- open zip file in c#
- Category
- C#
- Title
- c# format number with leading zeros
- Category
- C#
- Title
- office open xml check if row is empty
- Category
- C#
- Title
- check if network is available c#
- Category
- C#
- Title
- start up file in asp.net core
- Category
- C#
- Title
- c# query string builder
- Category
- C#
- Title
- C# copy string except for last letter
- Category
- C#
- Title
- .net core cli
- Category
- C#
- Title
- c# class declaration
- Category
- C#
- Title
- unity getkey keycode
- Category
- C#
- Title
- roulette algorithm genetic algorithm
- Category
- C#
- Title
- unity3d raycast
- Category
- C#
- Title
- array copy c#
- Category
- C#
- Title
- c# dictionary add
- Category
- C#
- Title
- f string C#
- Category
- C#
- Title
- how to look for substring in string in c#
- Category
- C#
- Title
- csharp attributes as generics constraints
- Category
- C#
- Title
- how to reference function in unity
- Category
- C#
- Title
- unity rotate vector
- Category
- C#
- Title
- .net core download image from url binary file
- Category
- C#
- Title
- hide inherited member c#
- Category
- C#
- Title
- c# index in select
- Category
- C#
- Title
- how to create a list c#
- Category
- C#
- Title
- the name scripts does not exist in the current context mvc 5
- Category
- C#
- Title
- serilog loglevel order
- Category
- C#
- Title
- How to look at an object unity
- Category
- C#
- Title
- c# remove last value from list
- Category
- C#
- Title
- change canvas color uwp c#
- Category
- C#
- Title
- c# format string to date yyyymmdd
- Category
- C#