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 cibine · Nov 17, 2009 at 02:56 PM · customization

Asset swopping script help (customization)

Hi all,

I have a query that I hope someone can help with. What I'm trying to achieve is this:

For my initial goal I have a range of assets in various states and I want to be able in the runtime to toggle through those states by using a single button press. Further to that I want to map a second button to do the same function on another set of assets. Thus for example lets say I have a series of car assets, on pressing button (1) i'd like to change through the wheel variants and button (2) the engine variants (3) bodywork etc.

My ultimate goal is to then be able to move to a different location in the environment and using the same buttons toggle say a bike and the other buttons its wheels, lamps etc without affecting the first set of choices I made on the car. So essentially for my button presses to be area specific.

I understand that this would require a trigger/ collision event upon which the specific buttons will be enabled for the specific functions. Once my player will exit the trigger area those, assigned functions will no longer apply for the buttons. However as I mentioned before it is important that the changes that occurred previously do not reset upon exiting the "effect area" or upon entering the new one. How can I do this please?

MM

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

2 Replies

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

Answer by Bampf · Nov 17, 2009 at 04:20 PM

When you say that changes that occurred previous should not reset, it's not clear how long you need this to last. If for example your entire game was organized as one Unity scene, any changes would naturally persist.

Assuming that this is not the case, what you want is to record somewhere what assets were selected, and then later on (when a new scene is loaded, or the next time the game is run) you want to recall what was saved.

The typical way to do this is for you to define a mapping for each of the possible selections. When the user selects blue bodywork for example, in your game you would map this to an identifier, typically a number. Then all you have to do is store the numbers somewhere, and read them back later.

So somewhere in your code you might have a function, let's call it "SelectBodywork". When given a number (representing the user's choice, whether because they clicked on a button just now or are loading the next scene) this function will set up the selected asset. For bodywork, maybe it just change a texture on the player's vehicle.

To store user selections and settings persistently you should have a look in the Unity documentation at the PlayerPrefs class.

Edit: In a comment you asked for detail on switching between the objects. Here's one way to do it.

Let's assume that you have 3 different car GameObjects already in the scene. 1) In your script declare a public array of GameObjects. (This script needs to be assigned to an active object in your scene, perhaps your button.) 2) In the Inspector, set the number of items in the array to 3. 3) Now drag your three car objects to each of the three elements of the array.

Now your code can walk the elements of the array, activating one game object, deactivating all others. When the user clicks, deactivate the current car and activate the next one in the list. If done properly you can edit the array in the editor, add or remove vehicles, without changing the code.

(You will probably also want to learn about prefabs. That's a whole other topic, but it would let you create GameObject templates for each of the different vehicles. Then you might have an array of the prefabs rather than needing each of the cars to be in the scene already. Then your code would call Instantiate on a prefab selected from the array. This let's you create the exact same vehicle in different scenes of your game, or for that matter have as many copies of the same car in the scene as you need.)

Comment
Add comment · Show 2 · 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 cibine · Nov 17, 2009 at 05:30 PM 0
Share

Hi, Thank you for the input. Sorry if the previous post was unclear. The actual question is how can I map a single key to switch between different objects. I can script different keys to switch objects on and off, but I can't quite grasp how it would be possible to use one key to toggle (loop) through a number of different states. ie. if I have 3 "hidden" objects, pressing key(1) sets object01.renderer.enabled = true for object01 and sets the value to false for object02 & 03. Pressing key(1) again enables the renderer for object02 but disables it for object01 and 03 and so forth. Thank you

avatar image Bampf · Nov 18, 2009 at 08:45 PM 0
Share

FYI, I added a new section at the end of my answer to try and answer this specific question.

avatar image
0

Answer by runevision · Nov 17, 2009 at 04:53 PM

To cover a different part of the question...

You want a button do do similar but not identical tasks on different objects - for example change 4 wheels for a car or 2 wheels for a bike.

You can do this by making two different scripts that implement the same interface. For example, you could make an interface called IWheelChanger. This interface could have a ChangeWheels function. You could then have two scripts, CarCustomizer and BikeCustomizer, that both implement the IWheelChanger interface, but do different things in their respective ChangeWheels functions.

Then, you can have a member variable of type IWheelChanger in the script that has the GUI, and the button can call the ChangeWheels function on the current object that is referenced in the variable.

You can switch the object stored in the member variable at any time, so it can be a bike at one time and a car at another time. The button will change the wheels of whatever object is stored there - the object just needs to implement the IWheelChanger interface.

You can learn more about interfaces in C# here. I don't know the syntax for JavaScript. You can post a new, seperate question about that if you need it.

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

2D Modular Customization With Snapping 1 Answer

Can I access a USB application through Unity? 3 Answers

how to change material color for player customization. 2 Answers

How to import the object from server to unity 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