Answer the question
In order to leave comments, you need to log in
I can't get a function from another script, whyyy?
I have a script where objects are created and I need to access it through Another script so that when the player touches it, some actions happen to it (I'll throw off the code), but when I try to access it, the following error occurs
NullReferenceException: Object reference not set to an instance of an object
RandomSpawn.SpawnRandom () (at Assets/Scripts/RandomSpawn.cs:19)
PlayerController.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/Scripts/PlayerController.cs:83)
RandomSpawn Spawn = new RandomSpawn();
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomSpawn : MonoBehaviour
{
public GameObject[] listObjects;
public GameObject Coin;
int random;
void Start()
{
random = Random.Range(0, listObjects.Length);
SpawnRandom();
}
public void SpawnRandom()
{
Instantiate(Coin, listObjects[random].transform.position, Quaternion.identity);
}
}
SpawnRandom
RandomSpawn Spawn = new RandomSpawn();
private void OnTriggerEnter2D (Collider2D collision)
{
if (collision.tag.Equals("Coin"))
{
HealthBar.fill += 0.2f;
Destroy(collision.gameObject);
Spawn.SpawnRandom();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question