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 SepM · Jun 15, 2015 at 09:53 PM · prefabcreatecopy

Prefab Variables Changing in the Original, Not the Copies

I've made a cube prefab and when it's created, it receives values from the main camera's script.

 var player : Transform;
 var thescript = player.GetComponent("Stats");
 var hitBoxCount : int;
 function Start () {
     Populate(1, 2, 1, 0, 0, -28);
     yield WaitForSeconds(1);
     Populate(2, 14, 2, 0, 0, -22);
 }
 
 function Populate(Player : int, Character : int, Team : int, x, y, z){    
     Instantiate(player, Vector3(x,y,z), transform.rotation);
     player.GetComponent("Stats").Player = Player;
     player.GetComponent("Stats").Character = Character;
     player.GetComponent("Stats").Team = Team;
 }

The values make it through, but only to the original prefab that I have in the Project panel, not the ones in the Hierarchy. I know this because whenever I start and restart the script, the original's values change from 0, to the first set of parameters of Populate, to the second and it just gets stuck there. I reset the values of the "Stats" script, but it does the same thing again. Does anyone have any idea how to just edit the "Clones" of a prefab and not the original?

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 DevsGoingViral · Jun 15, 2015 at 10:09 PM 0
Share

I think that function Start will not wait and that's all :)) Do another function with yield and then put that function in start.

function Start(){ plswork(); }

function plswork(){ Populate(1, 2, 1, 0, 0, -28); yield WaitForSeconds(1); Populate(2, 14, 2, 0, 0, -22); }

Does it work? :/

avatar image SepM · Jun 15, 2015 at 11:42 PM 0
Share

Thank you! That was a major help! Now I can finally move on with my project. Can you throw that in as an answer so that I can check it off for you?

2 Replies

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

Answer by William_Lee_Sims · Jun 16, 2015 at 05:39 AM

Do two things:

1) Remove the yield statement from your Start() function.

2) Assign your "Instantiate(...)" to another variable like "var pClone = Instantiate...". Then use that variable to do the rest: "pClone.getComponent("Stats").Player = Player;" and so on.

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
2

Answer by Bonfire-Boy · Jun 15, 2015 at 10:27 PM

So, player is your prefab, right? What you're doing is making a clone and then setting values on the object that you cloned, rather than on the clone. You need a reference to the clone, which is what's returned by Instantiate.

So replace line 11 with

Transform clone = Instantiate(player, Vector3(x,y,z), transform.rotation);

and then change the following lines to set values on clone rather than on player.

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

24 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

Related Questions

Instantiated coconuts not working 0 Answers

Using Instantiate on a Rigidbody rather than a whole GameObject 1 Answer

Creating Prefab causes scripts to break, Copy/Paste Gameobject doesn't 0 Answers

Instatiate prefab at another prefab issue 1 Answer

How could I create a prefab from an exsisting gameobject at runtime? 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