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 cmkbim · Apr 10, 2014 at 03:12 PM · prefab-instance

How do I set instance script parameters to a prefab?

I am attempting to make a game where the user can configure many instances of the same type of object/prefab but each instance in the game can have a different scale, color, texture, etc. How can I use the instantiate method the call my prefab to make a copy but then allow the script parameters to be modified on an instance based level rather than modifying all of the gameobjects of that type of prefab in the model? Just like when you make a class in C# with properties. You then instantiate that class multiple times and the values for properties on each of those instances may be different but the class itself is the same type of object. I'm a newbie so I know I am missing something here.

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 sumeetkhobare · Apr 11, 2014 at 02:24 PM 0
Share

The Instantiate function returns the instance of the GameObject you just instantiated. store it in a variable and using the Send$$anonymous$$essage function , send the properties to your gameobject you want it to have, like your color, scale,etc. you need to write down a function in the script attached to your gameobject to receive the properties in the Send$$anonymous$$essage function. And using this function itself, change the properties of your gameobject. So these changes will be only on the gameobject, not on the prefab, and in essence, not on the complete set of other gameobjects sharing the prefab.

If this helps, mark it as solved and tick my comment as the answer.

avatar image Leuthil · Apr 11, 2014 at 02:39 PM 0
Share

This comes back to the whole concept of Object Oriented Program$$anonymous$$g (using classes). The whole idea of "Instantiating" something is that it is creating a separate "instance" of that class. $$anonymous$$eaning it is on it's own and separate from the class. Anything you change in that instance won't affect anything else that derived from the same class.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MagicoCreator · Apr 11, 2014 at 02:42 PM

Instantiate returns a reference to the instantiated object, just store it in a variable and change it's values

for example:

 var MyInstance = Instantiate (MyPrefab, Vector3.zero, Quaternion.identity);
 MyInstance.transform.localScale = Vector3(Random.Range(1,1.1),Random.Range(1,1.1),Random.Range(1,1.1));

if you want to access your custom scripts you can get them with GetComponent:

 var MyInstance = Instantiate (MyPrefab, Vector3.zero, Quaternion.identity);
 var MyScript = MyInstance.GetComponent(MyScriptName);
 MyScript.MyValue = 1;
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

21 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

Related Questions

Why can't I undo changes to a prefab? (or to an instance of it after I apply the changes) 1 Answer

Prefab Canvas's buttons arn't working in different scene 4 Answers

How to undo prefab Apply? 0 Answers

I need to move a clone of a prefab to a specific position after it instantiates 1 Answer

How do I get the Player's Health Script as Component? 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