• 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 ger122 · Mar 08, 2015 at 01:11 AM · prefabs

Help! prefabs don't get counted in score when they get killed

I have tried everything my prefabs don't get counted in score when they get killed..

THIS IS 2D AND CSharp language here's the script- this script WORKS with gameobjects in the scene inspector already but doesn't work with prefabs!!

this does work on in scene gameobjects ,but not on prefabs, with prefabs the inspector for them wont let me put level manager" in the slot or drag it there..

why is this happening and please someone help me or post corrected script or tell me in detail what to do and what im doing wrong

 using UnityEngine;
 using System.Collections;
 
 public class playercscorecard : MonoBehaviour
   {
     public int increaseAmount; // how much will the count increase when the player hits enemy, one time only

     public LevelManager levelManager; // the instance of the LevelManager class. The empty game object which is attached with LevelManager script. Drag and drop the emptyGO.

     public float waitTime; // how much time is it going to take for the enemy to be destroyed after hit.

     private bool canStartCoroutine; // boolean to handle simple " one time only " logic   
         void Start()
     {
         canStartCoroutine = true; 
     }
               
     void OnCollisionEnter2D(Collision2D coll) {
         
         if (coll.gameObject.tag == "Enemy")
                   {
             if(canStartCoroutine)
             {
                 canStartCoroutine = false; // after one hit, boolean is made false immediately to prevent it from updating again.
                 StartCoroutine(WaitToDestroy(waitTime));
                              }
             }
              }
     IEnumerator WaitToDestroy(float time)
     {
         levelManager.killCount += increaseAmount; // increase the kill count by increaseAmount value
                  levelManager.SetKillCount(); // refresh the kill count
         yield return new WaitForSeconds(time); // wait for the desired time
         Destroy(gameObject); // destroy the gameobject itself.
     }
 }





Comment
Add comment · Show 2
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 ger122 · Mar 09, 2015 at 12:24 AM 0
Share

the code works on gameobjects in the scene just not on prefabs??

avatar image Pendantic · Mar 09, 2015 at 11:36 PM 0
Share

just a check you set increaseAmount to something besides zero on the prefab correct?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by badatnames16 · Mar 09, 2015 at 12:45 AM

I think you need to add a line in the "WaitToDestroy" IEnumertator to set the canStartCoroutine bool to true again.

Comment
Add comment · Show 9 · 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 ger122 · Mar 09, 2015 at 12:48 AM 0
Share

but it works on gameobjects just not on prefabs

avatar image ger122 · Mar 09, 2015 at 12:49 AM 0
Share

if I bring the prefab into the scene by dragging and then drag level manager to the prefab it will count it but some reason otherwise it wont

avatar image badatnames16 · Mar 09, 2015 at 12:52 AM 0
Share

are you applying the change when you put the level manager into it?

avatar image badatnames16 · Mar 09, 2015 at 12:53 AM 0
Share

also is it tagging the prefabs as "Enemy"?

avatar image ger122 · Mar 09, 2015 at 01:01 AM 0
Share

yes I have the prefabs tagged

prefabs are stored in a different area than gameobjects

when the prefabs are in their "prefabs" location folder I cannot put the level manager on them it wont let me drag anything on them in that area its just saying none. but when I drag the prefab to the scene I can then put the level manager on them and it lets me

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What prefab is my game object an instance of? 1 Answer

No "Standard Assets" in Project tab, new to unity, please help 2 Answers

Deleting instantiated prefabs clones? 2 Answers

Instantiating Multiple Prefabs at Runtime 0 Answers

spawn prefab in loop after collision? 2 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