- Home /
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
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.)
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
FYI, I added a new section at the end of my answer to try and answer this specific question.
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.
Your answer
