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 Tyler 2 · Nov 30, 2010 at 04:00 AM · tagreference

Exceptions to tag references?

Hello. I want to make a function that when activated it destroys all objects with a certain tag EXCEPT a few SPECIFIC objects that are currently in the scene, can I do this? (Yes, I know the obvious answer would be to change the tag for those objects, but that would mess things up, with the way I have my scripts and references). Thanks.

EDIT- This is the script I want to put Adams script into. But, when I do (like the script below it), I get errors. What is wrong?

var Bomb : GameObject;

var Player : Renderer; var Light : Light; var Timer : float = 3;

function OnTriggerEnter (other : Collider) { if (other.gameObject.CompareTag ("Enemy")) { Stats.Life -=1;

    Player.enabled = false;
    Light.enabled = false;
    Invoke("LightsOn", Timer);
   }; 

  if (other.gameObject.CompareTag ("Mul")) {
    Stats.Mul +=2;
    Destroy (other.gameObject);
    }

}

if( Input.GetButtonDown( "Fire1" ) ) {if (Stats.Bomb >= 1){ Instantiate(Bomb, transform.position, transform.rotation); Stats.Bomb-=1;}}

function LightsOn(){ Player.enabled = true; Light.enabled = true;}

This is my attempt to meld the scripts.

var savedObjects : GameObject[];

function OnTriggerEnter (other : Collider) { if (other.gameObject.CompareTag ("Enemy")) { Stats.Life -=1; Invoke("DeleteObjects"); }}

function SaveObject(go : GameObject) { var tempObjects : GameObject[]; tempObjects = new GameObject[savedObjects.Length + 1];

 Debug.Log(go.name);

 if(savedObjects.Length > 0)
 {
     for(i = 0; i < savedObjects.Length; i ++)
     {
         tempObjects[i] = savedObjects[i];
     }
 }
 tempObjects[tempObjects.Length - 1] = go;

 savedObjects = new GameObject[tempObjects.Length];

 for(i = 0; i < tempObjects.Length; i++)
 {
     savedObjects[i] = tempObjects[i];
 }

}

function ArrayContains(array : GameObject[], item : GameObject) : boolean { for(i = 0; i < array.Length; i ++) { if(array[i] == item) { return true; } } return false; }

function DeleteObjects() { for(var go in GameObject.FindGameObjectsWithTag("destroy")) { if(!ArrayContains(savedObjects, go)) { Destroy(go); } } }

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 BinaryCaveman · Nov 30, 2010 at 04:33 AM 0
Share

Do you already know the few specific objects, or are they deter$$anonymous$$ed at runtime?

avatar image Tyler 2 · Nov 30, 2010 at 04:59 AM 0
Share

I already know the specific objects.

1 Reply

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

Answer by Adam Rademacher · Nov 30, 2010 at 04:51 AM

You'll want to make a list, array, etc of references to the game objects you want to keep somehow. The safest way is probably in the inspector, though if some of the objects are instantiated, you'll want to add them to the list/array when they are added to the scene. Then run some code similar to this:

List<GameObject> savedObjects = new List<GameObject>();

foreach(GameObject go in GameObject.FindObjectsWithTag("DestroyMe")) { if(!savedObjects.Contains(go)) Destroy(go); }

Javascript functionality (tested now):

var savedObjects : GameObject[];

function SaveObject(go : GameObject) { var tempObjects : GameObject[]; tempObjects = new GameObject[savedObjects.Length + 1];

 Debug.Log(go.name);

 if(savedObjects.Length &gt; 0)
 {
     for(i = 0; i &lt; savedObjects.Length; i ++)
     {
         tempObjects[i] = savedObjects[i];
     }
 }
 tempObjects[tempObjects.Length - 1] = go;

 savedObjects = new GameObject[tempObjects.Length];

 for(i = 0; i &lt; tempObjects.Length; i++)
 {
     savedObjects[i] = tempObjects[i];
 }

}

function ArrayContains(array : GameObject[], item : GameObject) : boolean { for(i = 0; i < array.Length; i ++) { if(array[i] == item) { return true; } } return false; }

function DeleteObjects() { for(var go in GameObject.FindGameObjectsWithTag("destroy")) { if(!ArrayContains(savedObjects, go)) { Destroy(go); } } }

Call SaveObject and pass the game object to it when you instantiate an object or decide to save it. Don't call DeleteObjects() in Update as it is quite slow to FindGameObjectsWithTag every frame.

Comment
Add comment · Show 5 · 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 Tyler 2 · Nov 30, 2010 at 05:38 AM 0
Share

Would the script be like the one in my updated question?

avatar image Adam Rademacher · Nov 30, 2010 at 03:21 PM 0
Share

I will update my script portion to use javascript ins$$anonymous$$d.

avatar image Tyler 2 · Nov 30, 2010 at 11:25 PM 0
Share

Thanks. I have tried to meld that script with my existing script(as I did with my new edited question) I get errors, what am I doing wrong?

avatar image Adam Rademacher · Dec 01, 2010 at 01:47 AM 0
Share

What is the error you are getting?

avatar image Adam Rademacher · Dec 01, 2010 at 02:15 AM 0
Share

if( Input.GetButtonDown( "Fire1" ) ) {if (Stats.Bomb >= 1){ Instantiate(Bomb, transform.position, transform.rotation); Stats.Bomb-=1;}} needs to go into function Update() {}

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

No one has followed this question yet.

Related Questions

Using Tag to find Child and execute Scripts 1 Answer

how to stop tag switching on same object? 1 Answer

Not getting proper reference using Tags 1 Answer

Check Trigger Collision's Name/Tag 1 Answer

Set reference to multiple gameobjects based on tag category / multiple tags? 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