Syntax for OnTriggerEnter validation - c#

Why won't these if statements work? When the object with this script attached collides with the object named "Resistor" for example why won't it work?
Code:
private void OnTriggerEnter(Collider collider)
{
Debug.Log("AAAAAAAA");
if (collider.gameObject.name == "Resistor")
{
resistance = SliderScript.slider.value;
}
else if (collider.gameObject.name == "LDR")
{
LDRresistance = LDRSliderScript.sliderForLight.value;
}
else if (collider.gameObject.name == "Lightbulb")
{
Debug.Log("lol");
GameObject lightGameObject = new GameObject("Light spawned because of collision with a battery");
Light lightComp = lightGameObject.AddComponent<Light>();
lightComp.intensity = current;
lightComp.color = Color.white;
lightGameObject.transform.position = Lightbulb.transform.position;
Destroy(lightGameObject, 15);
}
else if (collider.gameObject.name == "Battery")
{
Debug.Log("Batteryname works");
Destroy(gameObject);
}
}
Thank you!

Just in case anyone else ever encounters this problem in the future:
The correct syntax is:
Private void OnTriggerEnter(Collider other)
{
If (other.gameObject.name == “whatever”)
{
Debug.Log(“anything”)
}
}

Related

Instantiate Spawning Hundreds of items instead of 1

I have written a script that detects when something collides with my game object. When something collides with it, the collision may last 1 - 2 seconds but I only want one game object to spawn. When I run the scene and collide with it hundreds of game objects spawn instantly and everything crashes.
I have tried using Thread.Sleep() and IEnumerator waitor() but no luck so far.
Any ideas would be appreciated, I will attach the code below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
public class ColliderHit : MonoBehaviour
{
public GameObject topLevelMenu;
public GameObject sublevel;
public GameObject topMenuItem;
public GameObject menuItemTouched;
public GameObject itemToSpawn;
public bool topLevelItem;
void OnTriggerEnter(Collider col)
{
if (col.gameObject.name != "" || col.gameObject.name == "RightHandVisual" || col.gameObject.name == "OculusHand_R" || col.gameObject.name == "HandGrabInteractor" || col.gameObject.name == "OVRHandPrefab")
{
if (topLevelItem)
{
topLevelMenu.SetActive(false);
sublevel.SetActive(true);
sublevel.transform.position = topMenuItem.transform.position;
sublevel.transform.rotation = topMenuItem.transform.rotation;
}
else
{
StartCoroutine(waiter());
var itemsPos = menuItemTouched.transform.position;
var itemsRot = menuItemTouched.transform.rotation;
var itemsSca = menuItemTouched.transform.localScale;
GameObject spawned = Instantiate(itemToSpawn);
spawned.transform.rotation = itemsRot;
spawned.transform.localScale = itemsSca;
var zpos = itemsPos.z - (0.1f);
spawned.transform.position = new Vector3(itemsPos.x, itemsPos.y, zpos);
}
}
}
IEnumerator waiter()
{
//Wait for 4 seconds
yield return new WaitForSeconds(4);;
}
}
Solution to fix your code as is
That's not how Co-routines work. The code that needs to be await needs to be within the coroutine:
bool isOnCollideCooldown = false;
void OnTriggerEnter(Collider col)
{
if (col.gameObject.name != "" || col.gameObject.name == "RightHandVisual" || col.gameObject.name == "OculusHand_R" || col.gameObject.name == "HandGrabInteractor" || col.gameObject.name == "OVRHandPrefab")
{
if (topLevelItem)
{
topLevelMenu.SetActive(false);
sublevel.SetActive(true);
sublevel.transform.position = topMenuItem.transform.position;
sublevel.transform.rotation = topMenuItem.transform.rotation;
}
else
{
// Return if it is instantiated
if (isOnCollideCooldown) return;
// Otherwise spawn and wait
StartCoroutine(waiter());
}
}
}
IEnumerator waiter()
{
// Go onto cooldown
isOnCollideCooldown = true;
// Spawn the object
var itemsPos = menuItemTouched.transform.position;
var itemsRot = menuItemTouched.transform.rotation;
var itemsSca = menuItemTouched.transform.localScale;
GameObject spawned = Instantiate(itemToSpawn);
spawned.transform.rotation = itemsRot;
spawned.transform.localScale = itemsSca;
var zpos = itemsPos.z - (0.1f);
spawned.transform.position = new Vector3(itemsPos.x, itemsPos.y, zpos);
//Wait for 4 seconds
yield return new WaitForSeconds(4);
// Go off cooldown
isOnCollideCooldown = false;
}
Additional Solution
The issue is that when you spawn the object, it instantly collides with the other objects. So on the script with the object that spawns you can use the following:
Collider col;
public float noCollisionTime = 1;
void Awake() {
col.enabled = false;
StartCoroutine(EnableCollider());
}
IEnumerator EnableCollider()
{
yield return new WaitForSeconds(noCollisionTime);
col.enabled = true;
}
You could try declaring a
bool isInstantiated = false;
and a method like this:
void ResetInstantiation()
{
isInstantiated = false;
}
then, check if it's already been instantiated:
else
{
StartCoroutine(waiter());
var itemsPos = menuItemTouched.transform.position;
var itemsRot = menuItemTouched.transform.rotation;
var itemsSca = menuItemTouched.transform.localScale;
if (isInstantiated) return;
GameObject spawned = Instantiate(itemToSpawn);
spawned.transform.rotation = itemsRot;
spawned.transform.localScale = itemsSca;
var zpos = itemsPos.z - (0.1f);
spawned.transform.position = new Vector3(itemsPos.x, itemsPos.y, zpos);
isInstatiated = true;
Invoke("ResetInstantiation", 4.0f);
}
This way, you set the value to true with stops the instantiation and reset the flag after 4 seconds. (or however many you wish)

Trying to get a variable changed on Collision

I'm trying to make flappy bird and I'm trying to make it when the bird hits the "floor" a variable changes and then the script for the movement is not able to go.
Kinda hard for me to explain but here is the code i have:
void Update()
{
void OnCollisionEnter2D(Collider2D col)
{
if (col.gameObject.tag == "floor") // || col.gameObject.tag == "Pipe")
{
active = 0;
}
}
if (active == 1)
if (Input.GetKeyDown("space"))
{
GetComponent<Rigidbody2D>().velocity = new Vector3(0, 10, 0);
}
}
That is my code ^
Please help : )
void Update()
{
if (active == 1)
{
if (Input.GetKeyDown("space"))
{
GetComponent<Rigidbody2D>().velocity = new Vector3(0, 10, 0);
}
}
}
void OnCollisionEnter2D(Collider2D col)
{
if (col.gameObject.tag == "floor") // || col.gameObject.tag == "Pipe")
{
active = 0;
}
}
This code works fine for me, make sure you add Rigidbody2d to the player + add the box collider and the tag to the floor
First of all you have the OnCollisionEnter2D nested under Update as a local function. Unity doesn't find and call it this way, it needs to be at class level.
And then you set
active = 0;
but check for
active == 1
this seems odd. I would expect you check for the same value.
In general rather use a bool flag:
private bool active;
// reference via the Inspector if possible
[SerializeField] private Rigidbody2D _rigidbody;
private void Awake()
{
// as fallback get it ONCE on runtime
if(!_rigidbody) _rigidbody = GetComponent<Rigidbody2D>();
}
private void Update()
{
if (active)
{
if (Input.GetKeyDown("space"))
{
// only change the Y velocity, keep the rest
// that's needed e.g. if you are moving sideways currently
_rigidbody.velocity = new Vector3(_rigidbody.velocity.x, 10);
// immediately disable jumping
active = false;
}
}
}
private void OnCollisionEnter2D(Collider2D col)
{
if (col.gameObject.CompareTag("floor") || col.gameObject.CompareTag("Pipe"))
{
active = true;
}
}
// also disable jumping when exiting just in case
void OnCollisionExit2D(Collider2D col)
{
if (col.gameObject.CompareTag("floor") || col.gameObject.CompareTag("Pipe"))
{
active = false;
}
}

Shooting extra balls - Block breaker game bug (Unity 2D)

I'm making a block breaker game that the player can get extra balls and shoot those balls with the original one.
Does anyone knows what is wrong? If there is not enough information, just tell me and I can provide it here.
Thanks
=========== CHANGED THE CODE TO BE MORE CLEAN ===========
Created my own pool logic to get more control and created a ExtraBallManager.cs to handle only functions related to that.
Here is how it is now (also I figured out that I didn't have to use coroutine):
public class ExtraBallManager : MonoBehaviour
{
private Ball ballController;
private GameManager gameManager;
public float ballWaitTime;
private float ballWaitTimeSeconds;
public int numberOfExtraBalls;
public int numberOfBallsToFire;
public ObjectPool objectPool;
//public TextAlignment numberOfBallsText;
// Start is called before the first frame update
void Start()
{
ballController = FindObjectOfType<Ball>();
gameManager = FindObjectOfType<GameManager>();
ballWaitTimeSeconds = ballWaitTime;
numberOfExtraBalls = 0;
numberOfBallsToFire = 0;
}
void Update() {
if(ballController.currentBallState == Ball.ballState.fire || ballController.currentBallState == Ball.ballState.wait){
if(numberOfBallsToFire > 0){
ballWaitTimeSeconds -= Time.deltaTime;
if(ballWaitTimeSeconds <= 0){
GameObject tempBall = objectPool.GetPooledObject("Temp Ball");
if(tempBall != null){
tempBall.transform.position = ballController.ballLaunchPosition;
tempBall.SetActive(true);
//It seems the error occurs in here
//sometimes it just don't add this tempBall to the
//list
gameManager.ballsInScene.Add(tempBall);
tempBall.GetComponent<Rigidbody2D>().velocity = ballController.initialBallVelocity;
ballWaitTimeSeconds = ballWaitTime;
numberOfBallsToFire--;
}
ballWaitTimeSeconds = ballWaitTime;
}
}
}
if(ballController.currentBallState == Ball.ballState.endShot){
numberOfBallsToFire = numberOfExtraBalls;
}
}
}
My Ball.cs part where switch the Ball State:
void Start()
{
arrow.SetActive(false);
currentBallState = ballState.aim;
rigidBody = GetComponent<Rigidbody2D>();
ballTransform = GetComponent<Transform>();
lineRenderer = arrow.GetComponent<LineRenderer>();
gameManager = FindObjectOfType<GameManager>();
stoppedPosition = transform.position;
currentBallState = Ball.ballState.aim;
gameManager.ballsInScene.Add(this.gameObject);
}
// Update is called once per frame
void Update()
{
switch(currentBallState) {
case ballState.aim:
if (Input.GetMouseButtonDown(0) && gameObject.tag == "Ball"){
MouseClicked();
}
if(Input.GetMouseButton(0) && gameObject.tag == "Ball"){
MouseDragged();
}
if(Input.GetMouseButtonUp(0) && gameObject.tag == "Ball"){
ReleaseMouse();
}
break;
case ballState.fire:
break;
case ballState.wait:
stoppedPosition = transform.position;
/*
if(gameManager.IsThereAnyExtraBallOnScene()){
currentBallState = ballState.endShot;
}*/
if(gameManager.ballsInScene.Count == 1){
currentBallState = Ball.ballState.endShot;
}
break;
case ballState.endShot:
for(int i = 0; i < gameManager.blocksInScene.Count; i++){
if(gameManager.blocksInScene[i]){
gameManager.blocksInScene[i].GetComponent<BlockMovementController>().currentState = BlockMovementController.blockState.move;
}
}
gameManager.level += 1;
gameManager.SpawnBlocks();
currentBallState = ballState.aim;
break;
default:
break;
}
}

Check if a set of objects is moving Unity 3D

I have several objects with the same tag, and I want to check if they are moving or not, to call a function when they're not moving. So I used the code bellow, but the aremoving is always false even when some objects still moving! Do you know what's wrong in my code?
Script:
bool aremoving;
void LateUpdate()
{
GameObject[] Cubes = GameObject.FindGameObjectsWithTag("Cube");
foreach (GameObject Cube in Cubes)
{
if (Cube.GetComponent<Rigidbody>() == null)
{
continue;
}
if (Cube.GetComponent<Rigidbody>().velocity.magnitude > 0.01)
{
aremoving = true;
}
if (Cube.GetComponent<Rigidbody>().velocity.magnitude <= 0.01)
{
aremoving = false;
}
}
Debug.Log("Cubes moving: " + aremoving);
}
Write the code like this
bool aremoving;
void LateUpdate()
{
GameObject[] Cubes = GameObject.FindGameObjectsWithTag("Cube");
foreach (GameObject Cube in Cubes)
{
if (Cube.GetComponent<Rigidbody>() == null)
{
continue;
}
if (Cube.GetComponent<Rigidbody>().velocity.magnitude > 0.01f)
{
aremoving = true;
}
Debug.Log("Cubes moving: " + aremoving);
aremoving = false;
}

OnCollisionEnter breaks when an else statement is added

In an attempt to create the basics of a game i'm creating in unity i have encountered a strange problem,
This code works just fine (materialstate is set to 0 in start()) and the object itself indeed changes material
void OnCollisionEnter(Collision col){
if (col.gameObject.name == "Player" && Materialstate == 0) {
renderComp.material = goal;
Materialstate = 1;
}
}
However as soon as i add an else statement it just breaks, the collision occurs( I know because i've checked it with a print statement for Materialstate in update()) But this time not even the initial collision(That was working fine before the else was added) works:
void OnCollisionEnter(Collision col){
if (col.gameObject.name == "Player" && Materialstate == 0) {
renderComp.material = goal;
Materialstate = 1;
} else {
if (col.gameObject.name == "Player") {
renderComp.material = starting;
Materialstate = 0;
}
}
}
Can you try this code out?
void OnCollisionEnter(Collision col) {
if (col.gameObject.name == "Player") {
if (Materialstate == 0) {
renderComp.material = goal;
Materialstate = 1;
return;
}
renderComp.material = starting;
Materialstate = 0;
}
}
This removes the else which basically checks for whether col.GameObject.name == "Player" twice. This might actually speed up your game code as well as that extra check is removed. It also performs the needed operations properly within the original condition (col.GameObject.name == "Player" is true).
It is written with the assumption that Materialstate will only be 0 or 1, however, if you plan on having more states, a switch statement might be your best bet in making this work as intended.
void OnCollisionEnter(Collision col) {
if (col.gameObject.name == "Player") {
switch (Materialstate)
{
case 0:
renderComp.material = goal;
Materialstate = 1;
break;
case 1:
renderComp.material = starting;
Materialstate = 0;
break;
default:
// Invalid Materialstate throw exception/error
}
}
}
I hope this helps!

Categories

Resources