Part 1 Homework -w3 -S4
Updated 2 months ago
My first time using a Global Variable! Hope I did it correctly.
Hi everyone, I finish the first part of the homework and before starting the challenge I want to share everything that I learned with some thoughts.

Display the total number of animals that have been fed in the console

My first thought when I read the homework was pice of cake. (How can I be so naive?) I opened my DetectCollisions.cs and just added:
int totalFedAnimals;
Then I simply added inside the onTriggerEnter:
totalFedAnimals = totalFedAnimals + 1;
And before even hit the Play button, it hit me, every time an animal prefab is spawned a new script will be created for that prefab meaning a new empty totalFedAnimal variable will be starting the count, just to be destroyed in the next very line.
totalFedAnimal = totalFedAnimal + 1; Destroy(other.gameObject); Destroy(gameObject);
So it won't work.
What I needed was a script that stayed all the time during the game. Thus I create a new Empty Object and named it Global Control, I also created a new script call ScoreManager. I recreate my variable there... and errors start to appear in the DetectCollisions.cs so I went to Saint Google and look around until I find that you can call a method in other scripts if you use static.
Then I went in my journey I create a new method inside the ScoreManager script.
public static void AddFeed(){ }
I went to the DetectCollisions.cs and call it inside OnTriggerEnter:
Came back to the new method and add the magic lines:
totalFedAnimal = totalFedAnimal + 1;
And new errors came to life. I try to solve them but I was clueless about many of the things that show up. So... I went back to Saint Google again but this time I looked in C# related solutions eventually, I found you can create a static whole class and store variables to be available to all scripts. (Here is what I read)
So in the ScoreManager I put static before class at the top then, once more, I came back to the DetectCollisions.cs and tried to type the name of my variable but it never appears in the box with the suggestions so I tried writing the class name dot the variable name and it worked, finally, I Play the game and it all happened as I wanted. Just to finish with the assignment I add a Debug.Log and I was finished.
private void OnTriggerEnter(Collider other) { ScoreManager.totalFedAnimals = ScoreManager.totalFedAnimals + 1; Debug.Log("Total feed animals: "+ScoreManager.totalFedAnimals); Destroy(other.gameObject); Destroy(gameObject); }
And my new script is:
using UnityEngine.SocialPlatforms.Impl; public static class ScoreManager { public static int totalFedAnimals; }
Next, I start writing this "short" post and looking at my project, I realise I never added the ScoreManager script to the empty Object I created so it turns out it doesn't need to be attached to work.
I hope this helps someone to achieve the assignment and if you find another way to do it please tell me.
Clau Dia
Clau Dia
2 months ago
JayGreat job! I could not for the life of me figure this out!
Thanks :D
2 months ago
Great job! I could not for the life of me figure this out!