Calling Function from another script not working
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:
https://www.youtube.com/watch?v=PrXLobhkNqU
And nothing seems to work. Appreciate any help given please!!
Answer by Larry-Dietz · Dec 29, 2017 at 11:40 PM
Are you dragging an object into the inspector for spawnNewCharacter? And if so, is this the spawn object you are wanting to call the SpawnCharacterMethod function?
If so, then in that 2nd if statement, change Name to spawnNewCharacter and I think you will be good to go.
Creating a new Spawn object like you are with Name will create an instance of the script NOT attached to a gameobject, and would result in a null reference error when trying to call the method.
I hope this helps, -Larry
Hi Larry, I think this will work, but just one small issue.
In the inspector, I can't seem to drag the object that I want to spawn in the spawnNewCharacter slot for Detect.cs:
I've tried dragging it into ObjectToSpawn but that doesn't work, I'm assu$$anonymous$$g it has something to do with spawnNewCharacter having the type 'Spawn'?
Anyway I get get around this?
For this to work, you need to have an object with the Spawn script attached, maybe an empty game object, and call it SpawnController or something. You would drag that object into the Spawn New Character field in the inspector.
Then you would drag the actual GameObject/prefab that you want to spawn into the Object To Spawn field in the inspector.
Doing this, SHOULD get you going.
There are few bugs that I should be able to fix within the hour, but that works perfectly, thank you!