OnTriggerEnter in another script - c#

Tell me please. I have a character. The character has a child element - sphere collider. This sphere is a trigger. There is a script that is located on the Terrain. How can I call the OnTriggerEnter function in this script which is located on the character sphere?
Terrain script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryManager : MonoBehaviour
{
public Transform Human;
// Update is called once per frame
void Update()
{
var humanTrigger = Human.Find("Trigger");
humanTrigger OnTriggerEnter(Collider other) {
print(other);
}
}
}

Related

How to increase score when clone prefabs touch wall?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CountScoreAtWall : MonoBehaviour
{
//Varriables
public GameObject[] prefabs;
public Text animalPassedText;
public int animalPassed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
animalPassedText.text = animalPassed.ToString();
}
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == tag)
{
animalPassed = animalPassed + 1;
Debug.Log("Animal Passed showed");
}
}
I want to let my clone Prefabs from Instantiate() touch the wall and then the score will Increase by 1 to animalPassedText but I don't know how to do that.
So please help me. ToT
Possible solutions:
1- Either the wall or your prefab should have a rigidbody to make OnTriggerEnter work. So check if one of your game objects has a rigidbody and they both have colliders.
2- Maybe your "tag" variable is not matching with collision.gameObject.tag, so if block is not triggered. Print collision.gameObject.tag and tag to see if they are matching.

Unity clone won't appear

I'm new to coding and i'm trying to spawn a clone of a ball in unity, but it won't appear.
It does spawn at the spawn point but it doesn't appear.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
public GameObject ball;
public Transform spawnPoint;
// Start is called before the first frame update
void Start()
{
SpawnBall();
}
// Update is called once per frame
void Update()
{
}
void SpawnBall()
{
Instantiate(ball, spawnPoint.position, Quaternion.identity);
}
}
Edit: it's a 3d game and the mesh renderer is enabled.
Okay nevermind: i just redid the ball and it worked.
¯_(ツ)_/¯

Bullet not destroying

I want my bullet to get destroyed OnTrigger, however it doesn't get destroyed but that Debug.Log works fine. I have tried everything such as rewriting the script, replacing it and attaching it over and over again. Could somebody help me?
Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public GameObject Bullet;
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Bullet"))
{
Debug.Log("I die");
Destroy(gameObject);
}
}
}
You're destroying the Enemy gameObject. To also destroy the bullet, try the code below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public GameObject Bullet;
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Bullet"))
{
Debug.Log("I die");
Destroy(gameObject); // destroying self object (Enemy Object)
Destroy(collision.gameObject); // destroying collided object (Bullet Object)
}
}
}
If you want to destroy the bullet you should pass the bullet object as an argument to the function Destroy(), but you are passing the enemy object.
replace Destory(gameObject) with Destroy(Bullet).
Or if you want to destroy the bullet that get triggered replace it with : Destroy(collision.gameObject)

Collision detection with character controller in Unity 3d

I have my player which uses character controller for moving. I placed a sprite in the scene and I'd like for when my player collides with the sprite to disable the sprite, like if the player grabs the sprite (which is Doom's 64 chainsaw).
The sprite's collisions of course work well with everything, but not with the player. How can I get proper collision between them?
You could do it like this:
1-Attach "Pickable" script to the sprite.
2-Attach "Player" script to the character controller.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickable : MonoBehaviour
{
public float radius = 1f;
private void Start()
{
SphereCollider collider = gameObject.AddComponent<SphereCollider>();
collider.center = Vector3.zero;
collider.radius = radius;
collider.isTrigger = true;
}
}
Here is the other script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
Pickable pickable = other.GetComponent<Pickable>();
if(pickable != null)
{
Destroy(other.gameObject);
}
}
}

How to properly make variable Public so it can be accessed by another script?

In Unity I have 2 GameObjects, a sphere and a capsule.
And I have a script attached to each.
Capsule script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CapsuleMesh : MonoBehaviour
{
public Mesh capsuleMesh;
void Awake()
{
capsuleMesh = GetComponent<MeshFilter>().mesh;
Debug.Log(capsuleMesh);
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Sphere script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeMesh : MonoBehaviour
{
Mesh mesh;
void Awake()
{
mesh = GetComponent<MeshFilter>().mesh;
Debug.Log(mesh);
}
// Start is called before the first frame update
void Start()
{
mesh = capsuleMesh;
}
// Update is called once per frame
void Update()
{
}
}
The mesh = capsuleMesh here is giving me an error about "the name capsuleMesh does not exist in the current context".
I thought that making capsuleMesh public in the other script would make THIS script be able to access it without issue.
What am I doing wrong?
capsuleMesh is a class variable defined in the CapsuleMesh class. It's not a global variable you can use everywhere. You need a reference to the instance of the CapsuleMesh class to retrieve the mesh stored in the capsuleMesh variable.
I've reworked your both scripts to make them work. I've spotted a flaw in your scripts. I guess ChangeMesh is meant to change the mesh of the gameObject? If so, you need to assign a new value to the meshFilter.mesh. Assigning a new reference to the mesh class variable is not enough (it would be pretty long to explain why)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CapsuleMesh : MonoBehaviour
{
public Mesh Mesh
{
get ; private set;
}
void Awake()
{
Mesh = GetComponent<MeshFilter>().mesh;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeMesh : MonoBehaviour
{
// Drag & drop in the inspector the gameObject holding the `CapsuleMesh` component
public CapsuleMesh CapsuleMesh;
private MeshFilter meshFilter;
void Awake()
{
meshFilter = GetComponent<MeshFilter>();
}
void Start()
{
meshFilter.mesh = CapsuleMesh.Mesh;
}
}

Categories