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 Kennardion · Oct 27, 2019 at 09:00 AM · instantiatescript.clones

About cloned scripts

Hi. I'm stuck on this problem for a while. Could someone help pls. I have a prefab which I clone a few a times on startup. After that I want to choose a random one and run the script inside of it but I don't know how to. Every time I want to access that specific script it just strats running in all of the clone instances. How can I access that particular script without triggering the others which I guess all have the same name.

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
1
Best Answer

Answer by naviln · Oct 29, 2019 at 12:26 PM

There is not a lot of info here, so I have made some assumptions.

  • In the prefab, whatever script you have inside it, don't put the code you want to run inside the start method (anything in start or awake gets run as the object is created and enabled). Put it in a separate method, and name it appropriately (for arguments sake, lets call it DoSomething())

  • Create a 'controller' class in your project.

  • Use this to initiate and control the 'cloning' of your prefabs. (Resources.Load())

  • As you clone each prefab, use GetComponent() to get a reference to its script.

  • Store this in a Dictionary or a List (Or you could store a reference to the Gameobjects themselves.)

  • Pick a random entry from the Dictionary or List, use GetComponent().DoSomething(insert any params if needed) to run the method of that specific cloned GameObject.


and some illustrative code for a 'controller' class:

             //a list to store the scripts
             List<SomeScript> scriptList = new List<SomeScript>();
 
             //instantiate a copy of a prefab
             GameObject go = (GameObject)Instantiate(Resources.Load("Prefabs/MyPrefab"), launchedPanels.transform);
             //grab the script component from it
             var script1 = go.GetComponent<SomeScript>();
             //and chuck it in our list
             scriptList.Add(script1);
             //repeat
             GameObject go2 = (GameObject)Instantiate(Resources.Load("Prefabs/MyPrefab"), launchedPanels.transform);
             var script2 = go2.GetComponent<SomeScript>();
             scriptList.Add(script2);
             //repeat a third time
             GameObject go3 = (GameObject)Instantiate(Resources.Load("Prefabs/MyPrefab"), launchedPanels.transform);
             var script3 = go3.GetComponent<SomeScript>();
             scriptList.Add(script3);
 
             //grab the first (in your case, you could write some code to pick a random element instead), and then run the DoSomething() method on it
             scriptList.FirstOrDefault().DoSomething();





The above is very crude - there is so much more that can be said, but ill leave it at that for simplicity. It's really just a sample to get you thinking.

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 Kennardion · Nov 11, 2019 at 11:21 AM 0
Share

Thx. I kinda did the same you suggested. After the gameobject has been cloned I put all of them in a list. And from that I can get the specific object I want and I just use getcomponent to call the script inside.

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

151 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 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 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 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 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 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 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

Tile game instatiation 1 Answer

Prefab issue - all appear at same location 2 Answers

instantiated gameobjects(clone) don't behave like its original gameobject. 1 Answer

My script only makes two clones. 3 Answers

Instantiate object as child of scripted objects parent 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