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 ChrisTylr · Oct 03, 2013 at 04:07 PM · destroycheckcountcollison

Detecting when certain objects have been destroyed

Ive tryed multiple variations of gameObject.tag in order to count how many times my bullet comes into contact with another gameObject of tag 'Finish', to i can then say when its hit twice, i can do something else. However its getting getting to 2.

Ive started with a variable 'var tagCount : int = 0;' and tryed all sorts of calculations to add 1 when a collision occurs with the tag, but it gets to 1 and wont add further.

My problem is i have 4 targets in game, but only 2 spawn in each time, and im really having trouble figuring this out.

 var noOfTargets : int =  4;
 var spawnTargetOne : int; 
 var spawnTargetTwo : int; 
 
 public var myFourTargets : GameObject[]; 
 
 function Start () {
 
     spawnTargetOne = Random.Range( 0, noOfTargets ); 
 
     var loopSize : int; 
     
     loopSize = noOfTargets - 1; 
     
     var howFarToTravel = Random.Range( 1, loopSize ); 
     
     var loopLand : int; 
     
     loopLand = spawnTargetOne + howFarToTravel; 
     
     loopLand = loopLand % noOfTargets;
     
     spawnTargetTwo = loopLand; 
     
     Debug.Log("the two safe and different numbers are " + spawnTargetOne +" "+ spawnTargetTwo);
     
     myFourTargets[spawnTargetOne].SetActive(true); 
     
     myFourTargets[spawnTargetTwo].SetActive(true); 
     
 }


How can i check if 'spawnTargetOne' and 'spawnTargetTwo' get destroyed, or come into conact with my bullet or whatever so i can write another function that will spawn one last target on a set location, and when thats destroyed end game?

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 tw1st3d · Oct 03, 2013 at 04:09 PM 0
Share

Ins$$anonymous$$d of destroying them, try doing setActive(false); based on their tag on collision. Destroy() isn't "bad practice" technically, but it's bad practice in my opinion. Takes more to completely destroy an object than to ignore it.

avatar image ChrisTylr · Oct 03, 2013 at 04:15 PM 0
Share

I have a different script doing the destroy than the script that is choosing which targets are active. So if i was to take your advice how would i access the array from the destroy script to say if collision occurs myFourTargets[spawnTargetOne].SetActive(false)?

2 Replies

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

Answer by ChrisTylr · Oct 03, 2013 at 04:54 PM

Ok, so before reading you answer (which to be honest don't have a clue how id write that in javascript) i managed to find a solution.

My score was working fine so i just built an extra digit around it, when target is destroyed i get 20 points and a certain variable goes up by 1.

in another script i call upon the variable and check to see when the value is 2, when it is it will spawn a target.

and even though i havnt got a target spawning yet, i did get the debug.log line when i wanted it, so its a good sign,

Looks like its sorted, although its probably not the best way, it works

Comment
Add comment · Show 1 · 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 tw1st3d · Oct 03, 2013 at 05:00 PM 0
Share
  • for posting your solution.

avatar image
0

Answer by tw1st3d · Oct 03, 2013 at 04:24 PM

So I was thinking something like this. Now, the code probably wont natively work, but it's a building block for what you might want to use.

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class CheckEnemy : MonoBehavior
 {
     public YourOtherClass yoc;
     public GameObject yourScriptObj;
     
     public void Start()
     {
         yoc = (YourOtherClass) yourScriptObj.GetComponent(typeof(YourOtherClass));
     }
     
     public void OnCollisionEnter(Collison e)
     {
         foreach(string player in YourOtherClass.playerList)
         // Not necessarily a string, could be anything. 
         {
             if(player.tag.Equals((GameObject) e.tag))
             {
                 player.setActive(false);
             }
         }
     }
 }
Comment
Add comment · Show 1 · 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 tw1st3d · Oct 03, 2013 at 04:24 PM 0
Share

Rephrase: This won't work natively.

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

How to keep score? 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

4 blocks destroyed - gravity true 2 Answers

Why does my counter keep going up after pickup is destroyed? 1 Answer

Script for counting number of Turrets and when I destroy all of them I go to the YOU WIN seine 1 Answer


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