I have my platform scripts where I want to detect collision with my camera
Camera has colliderer + istrigger = on
Platform has non kinematic body + collider and rigidbody.
I try to get reference to the other script but even the debug.log doesn’t get triggered.
The spawner object got the spawn script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Platform : MonoBehaviour
{
private Spawn spawn;
public GameObject spawner;
// Start is called before the first frame update
void Start()
{
spawn = spawner.GetComponent<Spawn>();
}
// Update is called once per frame
void Update()
{
transform.Translate(-Vector3.forward * Time.deltaTime);
if(spawn == null) {
Debug.Log("Spawn is null: " + spawn);
} else if(spawn.objsSpawned == null) {
Debug.Log("objSpawn is null: " + spawn.objsSpawned);
}
}
private void OnCollisionEnter(Collision other) {
// If onCollison cam enter
if (other.gameObject.CompareTag("MainCamera")) {
spawn.AddPlatform();
}
}
private void OnCollisionExit(Collision other) {
// If onCollsion cam exist
if (other.gameObject.CompareTag("MainCamera")) {
spawn.RemovePlatform();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawn : MonoBehaviour
{
public List<GameObject> objsSpawned;
public float objectDistance;
[SerializeField]
private float maxDistance;
void Start()
{
SpawnPlatform();
}
void Update()
{
}
// Spawn objects
public void SpawnPlatform()
{
Vector3 offset = transform.position;
for (int i = 0; i < objsSpawned.Count; i++)
{
offset += new Vector3(0f, 0f, objectDistance);
var obj = Instantiate(objsSpawned[i], offset, Quaternion.identity);
}
}
public void AddPlatform()
{
// If onCollison cam enter
for (int i = 0; i < objsSpawned.Count; i++) {
objsSpawned.Insert(objsSpawned.Count - 1, objsSpawned[i]);
}
Debug.Log("Enter");
}
public void RemovePlatform()
{
objsSpawned.RemoveAt(0);
Debug.Log("Exit");
}
}