I’m trying to fix this all day and I’m out of ideas why it’s not working…
I had it in a JS and translated it to C# because the bool I’m trying to access is in a C# script… (although i had the C# script in the standard asset folder and all that)… anyway it’s for a respawn in a multiplayer
the DamageScript is attached to the player that gets instantiated over the network, the SpawnScript is in the scene on a SpawnManager gameObject
this is the error I get:
NullReferenceException
UnityEngine.GameObject.GetComponent (System.String type) (at C:/BuildAgent/work/812c4f5049264fad/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:20)
damageSystem.Start () (at Assets/MyAssets/scripts/multiplayerScripts/damageSystem.cs:16)
this is the DamageSystem script:
using UnityEngine;
using System.Collections;
public class damageSystem : MonoBehaviour {
float hitPoints = 100.0f;
public AudioClip dieSound;
public Transform deadReplacement;
GameObject spawnManager;
SpawnScript Respawn;
void Start(){
spawnManager= GameObject.Find ("SpawnMananger");
Respawn = spawnManager.GetComponent("SpawnScript")as SpawnScript;
}
void ApplyDamage (float damage) {
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
return;
hitPoints -= damage;
if (hitPoints <= 0.0)
{
Respawn.Died = true;
Detonate();
}
}
void Detonate () {
Destroy (gameObject);
// Play a dying audio clip
if (dieSound)
AudioSource.PlayClipAtPoint(dieSound, transform.position);
// Replace ourselves with the dead body
if (deadReplacement) {
Transform dead = Instantiate(deadReplacement, transform.position, transform.rotation)as Transform;
// Copy position rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse(transform, dead);
}
}
static void CopyTransformsRecurse (Transform src, Transform dst) {
dst.position = src.position;
dst.rotation = src.rotation;
foreach (Transform child in dst) {
// Match the transform with the same name
Transform curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
}