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 /
  • Help Room /
avatar image
1
Question by Teh_Keeper · Mar 16, 2016 at 08:29 AM · instantiatevariablegetcomponentclone

Getting variable from instantiated clone instead of prefab C#

Here's the problem. I want to get variables from cloned object script, but instead i get variables of prefab's script. Code:

     public GameObject prefab;
     public Script script;
     public int value;

     void Start () {
     
     GameObject clone =(GameObject)Instantiate (prefab, transform.position, transform.rotation);
     script=(Script)clone.GetComponent<Script>();
     value=script.variable;    
     }

Editor indicates, that script indicates as [Prefab(Clone) (Script)], that means it refered to cloned object.

Comment
Add comment · Show 6
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 sandeepsmartest · Mar 16, 2016 at 10:15 AM 0
Share

Hi Teh_$$anonymous$$eeper , Can you explain in detail?? Below are the 3 ways of accessing variables across scripts 1. make a singleton of the class and access them across al the scripts 2. make the required variable static and use it in different scripts 3. other way is the way that you have used it in your example. Explain it in detail so that other developers can easily understand your question and can give you solution in no time. Hope this may help you.NS$$anonymous$$S

avatar image Teh_Keeper · Mar 16, 2016 at 11:23 AM 0
Share

The prefab script at start generates random values of variables. Then, after some time, another object, lets call it Spawner, instantiates prefab's clones with anther random values, and by this point it goes fine (cheked it in Editor). But when it comes to copy clones' variables to Spawner buffer, buffer receives original prefab's variables, that been generated at start, ins$$anonymous$$d of clones', that been generated later.

avatar image troien Teh_Keeper · Mar 16, 2016 at 12:04 PM 1
Share

So you mean that, you set a random value in the start of the prefab (and the clone should then also have another random value set in the start) but when reading it's value in the code above, the value is still that of the prefab ins$$anonymous$$d of the clone????

If not, ignore this :p

The code you have above instantiates a clone and then immediatly retrieves the variable. When creating a clone you create a copy of the prefab. With all values identical to the prefab at start.

I think that your problem is that Start is called just before the first frame update(read next, as the current frame has already started). Which means that if you read something from it in the same frame that you instantiated it, Start hasn't been called yet and you get the old values. To fix that you could use Awake ins$$anonymous$$d of Start.

If this was indeed the problem, please reply so that I can convert this into an answer

avatar image Teh_Keeper troien · Mar 16, 2016 at 03:19 PM 0
Share

Sry, it didn't help. Also, i made a mistake. It's Update ins$$anonymous$$d of Start, and part of the code looks like that:

'void Update () {
     if(trigger){
     GameObject clone =(GameObject)Instantiate (prefab, transform.position, transform.rotation);
     script=(Script)clone.GetComponent
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ComradeVanti · Mar 16, 2016 at 10:19 AM

Well mabye you get the correct variables but they just have the same value on the clone and prefab. Is that a possibility?

Comment
Add comment · Show 3 · 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 Teh_Keeper · Mar 16, 2016 at 11:11 AM 0
Share

No. I'm making 3 different clones simultaneously, and values inside is different in all clones and prefab.

avatar image ComradeVanti Teh_Keeper · Mar 16, 2016 at 04:22 PM 0
Share

Alright. Could you give a more detailed discription on what you are trying to achieve? I mean really discribe the situation. What kind of variables are we talking about here? Why do you need random values each time you instantiate a gameobject. If you give us more info mabye we can find a workaround

avatar image Owen-Reynolds · Mar 16, 2016 at 05:32 PM 0
Share

Yes -- the code show is getting the value from the Instantiated object. The error would be using prefab.GetComponent(script), but the code shown correctly uses clone.GetComponent.

There's something else wrong, in code not being shown, that's supposed to do something not being described what it is. I don't think anyone can help with that in this Q.

avatar image
0

Answer by giorashc · Mar 16, 2016 at 05:46 PM

When instantiating an instance from a prefab the instance variables will have the same values as the prefab. You will have to set the variables on the clone after it has been created via your script.

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

48 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

Related Questions

Edit variable in instantiated clone 1 Answer

For loop and Instantiate issue 2 Answers

How do you make a clone have the same tag as its original? 0 Answers

My health bars image.fillAmount doesnt change. 4 Answers

Bullet clones will not be destroyed. 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