Hi, I’m learning coding in C# in Unity. I’ve got a Will Goldstone Book about Unity. But I have a little problem. I created ‘Create Empty’ Game Object as a Spawn Point for my First Aid Capsule. In this Empty game object ( firstManager ) I attached that script: //firstManager have tag: firstAidC
using UnityEngine;
public class firstManager : MonoBehaviour
{
public PlayerHealth playerHealth;
public GameObject firstAid;
public float spawnTime = 1f;
public Transform[] spawnPoints;
public bool exist = false;
void Start ()
{
InvokeRepeating ("Spawn", spawnTime, spawnTime);
}
void Spawn ()
{
if (playerHealth.currentHealth <= 0f)
{
return;
}
if (exist != true)
{
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
Instantiate (firstAid, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
}
exist = true;
}
}
And at ‘FirstAidCapsuleRotator’ ( this is a prefab ) i’ve:
using UnityEngine;
using System.Collections;
public class FirstAidCapsuleRotator : MonoBehaviour {
GameObject player;
PlayerHealth playerHealth;
bool playerInRange;
public int howMuchHeal=20;
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player");
playerHealth = player.GetComponent <PlayerHealth> ();
}
void OnTriggerEnter (Collider other)
{
if(other.gameObject == player)
{
playerInRange = true;
}
}
void OnTriggerExit (Collider other)
{
if(other.gameObject == player)
{
playerInRange = false;
}
}
void Update ()
{
transform.Rotate (new Vector3 (20, 10, 0) * Time.deltaTime * 10);
if(playerHealth.currentHealth < 100 && playerInRange)
{
Heal ();
}
}
void Heal ()
{
if(playerHealth.currentHealth < 100)
{
playerHealth.Healed(howMuchHeal);
Destroy (gameObject);
//GameObject.FindWithTag("firstAidC").GetComponent<firstManager>().exist = false;
}
}
}
And now in ‘firstManager’ script i’ve:
if (exist != true)
{
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
Instantiate (firstAid, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
}
exist = true;
At the end, Variable ‘exist’ is ‘true’ so condition is done.
I want change ‘exist’ variable from FirstAidCapsuleRotator. I try using
GameObject.FindWithTag("firstAidC").GetComponent<firstManager>().exist = false;
but it didn’t work.
When player raises capsule it is disappear, but i have error
‘Object reference not set to an instance of an object’ at this line:
GameObject.FindWithTag("firstAidC").GetComponent<firstManager>().exist
Sorry for my english, and noobish question
All scripts come from the project ‘Nightmare Project’
Thanks