- Home /
How to apply check on an instanced prefab
Hello,
I have the AI.cs script that keeps track when a unit dies etc. I spawn a clone for the 'RightGrunt' prefab named 'RightGrunt(Clone)' (in Hierarchy)
How can I tell Unity :" For this spawned prefab, please give me 60 gold when it dies"? This question is bugging me for the second week by now.
I tried in a separated script to check for the spawned unit like this :
void Update () {
AI ai = GetComponent<AI>();
if (ai.dead){
Debug.Log("#################################################");
if (gameObject.name == "RightGrunt(Clone)"){gold += 60;
}
}
}
Without any success (it stops at the gameObject.name check).
I don't know what to do anymore. I tried at least 12 different methods to make it work without any success.
Someone please help me. I'm prepared to even write a new gold reward script just to make this work.
Thank you for your time
Answer by Tomer-Barkan · May 23, 2013 at 09:20 PM
This depends how you keep track of your gold.
One way would be to have a static variable, and then it would be as simple as MoneyManager.gold += 60
.
If this is not good enough you'll have to provide more details of how your code works.
I have a script named displaygold.cs that has the lines needed to display the gold. Gold should be a static variable in order to access it from another script as you said $$anonymous$$oney$$anonymous$$anager.gold += 60?
That's the easiest way. If you have a constant amount of gold counters that's the best way (you can create an array of counters if there is more than one).
The harder way, if you have an unknown amount of gold counters, or your gold counter per player object, then you can modify the value in the player script directly. First, fetch the player GameObject
:
GameObject playerGameObj = GameObject.Find(killingPlayerName);
then fetch the script from the player that contains the gold counter:
PlayerScript playerScript = playerGameObj.GetComponent<PlayerScript>();
then modify the values (best to call a public method in the $$anonymous$$oney$$anonymous$$anager to do that):
playerScript.AddGold(60);
Here is my code. I try even without the damn 'if name' check. Just a simple.. if any unit dies then give gold.
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);
displayhealthgold=(displayhealth)playercam.GetComponent("displayhealth");
gold.gold=gold.gold+60;
Debug.Log("Fuuuuuuucckkkkkk");
}
Everything works up to Fuuucckkkkk. Though I don't see my gold modifing. Something is not working there (I get no error). I will provide whatever is needed for extra clarifications.
what is gold.gold
? Show me how you store the gold, and how you display it.
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class displayhealth : $$anonymous$$onoBehaviour {
//GOLD AND HEALTH POTIONS
public int gold;
public int startinggold=200;
public bool enablemenu=true;
public Transform playercam;
// Use this for initialization
void Start () {
InvokeRepeating("$$anonymous$$oreGold", 1, 1F);
gold=startinggold;
}
// Update is called once per frame
void Update () {
}
void $$anonymous$$oreGold() {
if (Time.timeScale>0){
gold += 1;
}
}
void OnGUI(){
if(GUI.Button(new Rect(0, 120, 130, 26), "Gold: "+gold)){}
}
}
Answer by darthbator · May 24, 2013 at 10:16 PM
I actually didn't read most of this but I am sort of confused as to why this would be an issue. Just contact whatever inventory manager in the method you are using to "tear down" the grunt object. Generally if there is only a single instances of the inventory class I'll just pack the entire class into a singleton and then let the other objects access it through that rather then making individual static variables or using access through the component system.
http://wiki.unity3d.com/index.php/Singleton
^ I highly recommend reading through this as singletons are AWESOME in unity IMO.
It should've been simple I guess as I managed to solve more difficult things, but for the love of god I don't understand why Unity wont just recognize a spawned prefab.
It was simple. $$anonymous$$y god....Thank you very much for the help guys, especially with the singleton thing.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Managing gui with prefabs 2 Answers
Instantiate gui.Text issue 1 Answer
change prefabs with Gui.buttons 0 Answers