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 SupremeRussian · Feb 23, 2014 at 12:37 AM · gameobjectprefabtaginstancefind

Affect All Instances Of A Prefab

Hey guys, thanks for taking a look at my question. Basically, I am using the

 gameObject.Find("Object").GetComponent(Script).enabled=false

thing. I want to disable a script on all instances of a prefab, but this only affects the main object, not everything.

 gameObject.FindWithTag

doesn't work either. To sum up, I would like to find a way to disable a component, or this case a script, on all instances of a prefab. Thank you :D.

Comment
Add comment · Show 3
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 sdgd · Feb 23, 2014 at 12:40 AM 0
Share

please format your code.

Next time you'll post A or Q highlight your code and press 101010 button, it's easier to read.

avatar image SupremeRussian · Feb 23, 2014 at 12:43 AM 0
Share

Ty sdgd for editing XD. Sorry for my improper manners, I am new.

avatar image sdgd · Feb 23, 2014 at 12:50 AM 0
Share

well usually I point to tutorial on the right but today it's not working.

usually I don't edit, because I didn't have that powers before.

3 Replies

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

Answer by sdgd · Feb 23, 2014 at 12:48 AM

there are a coupple of ways you can do this:

1 is tagging all your your objects a certain prefab.

and than call it:

 GameObject[] GOs = GameObject.FindGameObjectsWithTag("TagName");
 // now all your game objects are in GOs,
 // all that remains is to getComponent of each and every script and you are good to go.
 // to disable a components
 for (i=0; i<GOs.Length; i++){
     // to access component - GOs[i].GetComponent.<BoxCollider>()
     // but I do it everything in 1 line.
     GOs[i].GetComponent.<BoxCollider>().enabled = false;
 }



another method is to use static variables or for your case function.

 static void FunctionToDeactivateAllScripts(){
     this.enabled = false;
 }

now when you call this function from anywhere ALL scripts containing this function will be disabled, because only 1 function exists.

Comment
Add comment · Show 4 · 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 SupremeRussian · Feb 23, 2014 at 12:53 AM 0
Share

Hmmm, the second option won't work in my case. But the first option, what do you mean by "tag" all my objects. All the objects are instances of a prefab. And where would I put that line of code.

avatar image sdgd · Feb 23, 2014 at 12:58 AM 0
Share

you put that line of code anywhere you want, it'll search entire Hiearchy afaik only active GameObjects.

and by tag I ment in inspector top left you see tag.

default tag is untagged.

to change tag either change tag of your prefab in inspector OR everytime you instantiate a prefab with:

 gameObject.tag = "YourTagName";

OFC don't forget to add tags, ...

avatar image SupremeRussian · Feb 23, 2014 at 01:01 AM 0
Share

Ok ty man :D

avatar image KarlKarl2000 SupremeRussian · Apr 09, 2016 at 01:27 PM 0
Share

Oh $$anonymous$$an this saved my life!

I was wondering why my 1st prefab was only being affected by the script. This for loop was the answer!

Thank you sdgd!!!!!

avatar image
0

Answer by Kibsgaard · Feb 23, 2014 at 01:05 AM

You could try (C#):

 GameObject[] gos = GameObject.FindGameObjectsWithTag("TagOnObjects");
 foreach(GameObject go in gos)
 {
        go.GetComponent<NameOfScript>().enabled = false;
 }


If you for some reason need to change it through prefabs, take a look at PrefabUtility.

Comment
Add comment · 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
0

Answer by ReContraChanfle · Feb 23, 2014 at 03:41 AM

if you disable the script in only one prefab, that affects all other.

something like this:

 if(something)
 Object(Prefab).GetComponent<Script>().DoSomething();
Comment
Add comment · 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

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

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

find in his own instance 0 Answers

Identifying instances of prefabs in code 1 Answer

Finding a prefab in after respawn 1 Answer

Whats the best way to access other Game Objects? 2 Answers

How can I modify a variable on an instance of a prefab after I created it? 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