Hi there, I have a Spawn class that spawns a character in my 3D game, and transforms it to a certain point in the game, like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawn : MonoBehaviour {
public Object ObjectToSpawn;
public float speed = 5;
void Start () {
}
public void SpawnCharacterMethod() {
// spawns and moves character to a point
Instantiate(ObjectToSpawn, transform.position, transform.rotation);
}
public void SpawnCharacter() {
// calls method
SpawnCharacterMethod();
Debug.Log("NPC Spawned!");
}
}
I then have a Detect class, and when the collision detection goes off in the ‘Detect.cs’, I need to call the SpawnCharacterMethod()
again that is in the ‘Spawn.cs’. Here’s how I’ve tried to call the SpawnCharacterMethod()
function in the Detect.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Detect : MonoBehaviour {
Animator anim;
int jumpHash = Animator.StringToHash("Bored");
public Spawn spawnNewCharacter;
public Object ObjectToSpawn;
Spawn Name = new Spawn();
void Start() {
anim = GetComponent<Animator>();
}
void Update(){
}
void OnCollisionEnter(Collision col) {
if (col.gameObject.name == "Target1") {
anim.SetTrigger(jumpHash);
Debug.Log("Play touched plane!");
}
if (col.gameObject.name == "Target1")
{
Debug.Log("Spawn?"); // this displays in the console!!
Name.SpawnCharacterMethod(); // but this doesn't call
}
}
}
As you can see in the second if statement
in the Detect.cs, I’ve placed a debug that checks whether the if statement has been entered, it gets entered, the debug.log displays on console, but the function doesn’t get called?
I’ve even tried this method for calling functions in different classes:
And nothing seems to work. Appreciate any help given please!!