Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 GereBear · Oct 05, 2011 at 07:30 PM · componentdestroyimmediate

Component Find and Replace: execute's fine, fails to do anything?!

I was originally writing a lot my code in Javascript (2 different scripts) but decided to switch all of them over to one C# script.

The scripts were originally called Waypoint and WaypointDual. I need to replace all instances of those two components with my script called BoardPoint.

I wrote a small script (below) to iterate through GameObjects (sequentially numbered 0 to 104 (and #a and #b in some cases (following a WaypointDual))

When I toggle the 'destroyWP' in the inspector, it toggles back off (correctly), the 'destroyed' variable toggles to true, and my log fills with 105 entries of destroyed....

Problem: They all still have an instance of Waypoint (or WaypointDual)... any ideas?



@script ExecuteInEditMode
    public var destroyWP = false;
    public var destroyed = false;
function Update () 
{
    if(destroyWP == true)
    {
        for(var i:int = 0; i < 105; i+=1)
        {
            var go = GameObject.Find(i+"");
            if(go == null)
            {
                var goA = GameObject.Find(i + "a");
                var goB = GameObject.Find(i + "b");
                goA.DestroyImmediate(GetComponent (Waypoint));
                goB.DestroyImmediate(GetComponent (Waypoint));
            }
            else
            {
                go.DestroyImmediate(GetComponent (Waypoint));
                go.DestroyImmediate(GetComponent (WaypointDual));
            }
            Debug.Log("Waypoint on Object "+i+" or "+i+" a|b should be destroyed...");
        }
        destroyWP = false;
        destroyed = true;
    }
}

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

1 Reply

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

Answer by Bunny83 · Oct 05, 2011 at 07:47 PM

DestroyImmediate or Destroy are static functions and you have to provide a reference to the object you want to destroy. GetComponent on the other side is not a static function. It requires an instance of a GameObject or a Component on which it searchs for the component.

If you call GetComponent within a MonoBehaviour script it will search on the GameObject this script is attached to (it's the same as this.Getcomponent()).

         if(go == null)
         {
             var goA = GameObject.Find(i + "a");
             var goB = GameObject.Find(i + "b");
             DestroyImmediate(goA.GetComponent (Waypoint));
             DestroyImmediate(goB.GetComponent (Waypoint));
         }
         else
         {
             DestroyImmediate(go.GetComponent (Waypoint));
             DestroyImmediate(go.GetComponent (WaypointDual));
         }

Also keep in mind that DestroyImmediate ONLY works in the editor. Never use it ingame! Same rule applies the other way round: Don't use Destroy in an editor-script.

You should actually use a true editor script like this (not tested i'm a C# guy :D):

 // UnityScript (JavaScript)
 class DestroyWaypoints extends Editor
 {
     @MenuItem ("MyMenu/DestroyWaypoints")
     static function DestroyWPs () 
     {
         for(var i:int = 0; i < 105; i+=1)
         {
             var go = GameObject.Find(i+"");
             if(go == null)
             {
                 var goA = GameObject.Find(i + "a");
                 var goB = GameObject.Find(i + "b");
                 DestroyImmediate(goA.GetComponent (Waypoint));
                 DestroyImmediate(goB.GetComponent (Waypoint));
             }
             else
             {
                 DestroyImmediate(go.GetComponent (Waypoint));
                 DestroyImmediate(go.GetComponent (WaypointDual));
             }
             Debug.Log("Waypoint on Object "+i+" or "+i+" a|b should be destroyed...");
         }
     }
 }

btw if you just want to delete all Waypoint and WaypointDual scripts just do it that way:

 var allWaypoints = FindObjectsOfType(Waypoint);
 for(var Obj in allWaypoints)
     DestroyImmediate(Obj);
 
 var allWaypointDuals = FindObjectsOfType(WaypointDual);
 for(var Obj in allWaypointDuals)
     DestroyImmediate(Obj);
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 GereBear · Oct 05, 2011 at 08:00 PM 0
Share

I'll try that out; even if it doesn't work quite right (i can at least figure it out from here.

FindObjectsOfType: was looking for this all morning, didn't see it, ty for that :)

avatar image Bunny83 · Oct 07, 2011 at 12:27 PM 0
Share

I'm interested in what "doesn't work quite right"?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Help Trying to translate cs to js problem with one block of code 2 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

How to destroy linked components when object is destroyed? 1 Answer

Using Mechanim for FP Animation - Help 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