Hello everyone,
This problem is bugging me for the last two weeks. Please I really need help to sort it out.
I have the health.cs script set on my prefab “RightGrunt” :
public void Update () {
//tell ai when health is changed
if(healthsave>currenthealth|healthsave<currenthealth){
AI ai=(AI)GetComponent("AI");
if(ai){
ai.health=currenthealth;
healthsave=currenthealth;
}
}
//death
if(dead){
AI ai=(AI)GetComponent("AI");
if (ai !ai.dead){ // if ai.dead not set yet...
ai.dead = true; // set it and destroy capsule
Destroy(transform.Find("Capsule").gameObject);
Debug.Log("Fuuuuuuucckkkkkk");
}
//tell ai that he is dead
if(ai)ai.dead=true;
displayhealth gold = GetComponent<displayhealth>();
//gold.gold=gold.gold+60;
gold.gold+=60;
}
else {
}
and the displayhealth.cs script that handles the gold mechanics and display, it is added to my Camera :
public class displayhealth : MonoBehaviour {
//GOLD
public int gold=10;
public int startinggold=200;
public Transform playercam;
public Transform enemylist;
// Use this for initialization
void Start () {
InvokeRepeating("MoreGold", 1, 1F);
gold=startinggold;
}
// Update is called once per frame
void Update () {
}
void MoreGold() {
if (Time.timeScale>0){
gold += 1;
}
}
void OnGUI(){
AI enemys=(AI)enemylist.transform.GetComponent("AI");
if(GUI.Button(new Rect(0, 120, 130, 26), "Gold: "+gold)){}
}
}
My first step of the problem. When any AI dies, I can’t trigger the give gold (gold.gold+=60; or gold.gold=gold.gold+60; ). It give me this ERROR when AI dies: Object reference not set to an instance of an object… and points to this line: gold.gold+=60;
The strange thing is that I have a similar line of code in my shop.cs script and that works flawlessly :
displayhealth gold=(displayhealth)playercam.GetComponent("displayhealth");
if(gold.gold>=50){
clone1 = Instantiate(prefabObject1, Spawner.transform.position , Spawner.transform.rotation) as GameObject;
gold.gold=gold.gold-50;
Please help me.
Thank you in advance.
As you said yourself you applied displayhealth to your main camera, and the ai script is on RightGrunt.
Now if the ai dies it will look for displayhealth on the RightGrund object and wont find it so gold is null which you will realise when you try to use it a line below.
Makes sense. So what do you propose ?
- Somehow modify the script so it recognises the dead AI?
- Add the displayhealth script to the prefabs as well ?
I need to take into consideration some things. Which method will guarantee that when I will have different prefabs spawned the script will sill work?
Also how can we explain the fact that shop.cs works and it is also on a different object than the displayhealth.cs script?
Could the problem be because for the displayhealth.cs, when I check in the Inspector, the camera object is pointed to be the one in the hierarchy and for the health.cs it shows the object prefab named Camera (I had to se the main camera as prefab because the inspector wasn’t allowing me to use the Camera in the hierarchy for the health.cs which is attached to the RightGrunt prefab)?
Somehow you need to get a reference to displayhealth…maybe use statics or gameobject.find.
Ok, I managed to get both problems solved which is like a miracle or something. A big cart of thanks goes to you Arterie.
Though, I still have a small issue. Attaching the displayhealth,cs script to my prefabs has the problem that in the moment when I spawn multiple prefabs, my GUI and gold instantiate proportional to the spawned number.
Any idea how can I create a script that groups all of my gold from each spawned prefab and displays it correctly ?
Right now I modified the health.cs script like this and attached together with displayhealth,cs on the same prefab (unit):
if(dead){
AI ai=(AI)GetComponent("AI");
if (ai !ai.dead){ // if ai.dead not set yet...
ai.dead = true; // set it and destroy capsule
Destroy(transform.Find("Capsule").gameObject);
displayhealth gold = GetComponent<displayhealth>();
if (gameObject.name == "RightGrunt(Clone)"){gold.gold+=60;}
}
//tell ai that he is dead
if(ai)ai.dead=true;
}
Yes, now I remember why I didn’t liked programming. There are some small things that can make a huge difference and if you don’t know/see them, you will struggle in vain 
I solved my long lasting problem by reading about static variables. After 5 minutes everything was working perfectly.
Lots of thanks again Arterie. Problems is solved and closed !