Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by primus88 · May 23, 2013 at 08:07 PM · guiinstantiateprefabs

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image primus88 · May 24, 2013 at 06:37 AM 0
Share

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?

avatar image Tomer-Barkan · May 24, 2013 at 07:00 AM 0
Share

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);
avatar image primus88 · May 24, 2013 at 09:27 PM 0
Share

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.

avatar image Tomer-Barkan · May 24, 2013 at 09:41 PM 0
Share

what is gold.gold? Show me how you store the gold, and how you display it.

avatar image primus88 · May 24, 2013 at 09:56 PM 0
Share

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)){}
 
                 }
     
     }
Show more comments
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image primus88 · May 24, 2013 at 11:10 PM 0
Share

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.

avatar image primus88 · May 25, 2013 at 08:26 PM 0
Share

It was simple. $$anonymous$$y god....Thank you very much for the help guys, especially with the singleton thing.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges