- Home /
Changing material with button press
I'm new to Unity and I picked the Roll a Ball tutorial and started modifing it just to learn. I've set up some menus and all that but I want to have a menu with multiple buttons that change the material of the ball. So that each button corresponds to a different material, like a character.
Anyone knows how I can achieve this? I'm new to coding and all that so I would apreciate all the help I can get
-Wanderer
Answer by Penca53 · Sep 30, 2017 at 06:36 PM
You can use an array variable which stores all the material you want to change to. //Example
public Material[] materials;
private MeshRenderer meshRenderer;(or you can create a public variable and add the MeshRenderer in the editor)
void Start () {
meshRenderer = GetComponent<MeshRenderer>();
}
void Update () {
if (Input.GetKeyDown(KeyCode.Key)){
meshRenderer.material = materials[number];
}
}
Answer by FernandoGBR · Sep 30, 2017 at 06:37 PM
For the buttons to show: - Add some UIButtons (right clicking) to the canvas you did. - Place them in the proper position and name them as u wish.
For the buttons to work.
Create a new empty gameobject, name it materialController.
Add a new script to the materialController call it MaterialController (MC)
In the MC script, you need 3 things: referencing the materials, referencing the ball, a function to be called fro mthe buttons.
Referencing the materials and the ball:
public GameObject ballObject;
public Material[] materials = new Material[3]; //for 3 materials
Add the above code at the start of the MC. Go back to unity editor and drag your ball and your materials to the script.
Making the function to work
public void changeMaterial(int index){
if(index >= materials.Count) return; //Checking the material is in the list
ballObject.getComponent<MeshRenderer>().material = materials[index];
}
Lastly, go back to unity editor and select all your buttons. Go to the button component in the inspector, in the bottom part find the "on click ()", press "+". Drag your MC to the gap, search your funcion inside the new dropdown list. If you have done it correctly, a new input field will appear for you to enter the int number that the button will pass to the function. Select 0 for the 1st texture, 1 for the 2nd, and keep going.
Maybe there are some typos, but you can get an idea of how to make it work.
Thank you for your reply, but unfortunately I have some problems.
How can I drag the ball (player) that is on the scene with the actual game to the scene where I have the buttons with the script?
I copied your script but It's giving me an error:
Assets/Scripts/$$anonymous$$aterialController.cs(12,26): error CS1061: Type UnityEngine.$$anonymous$$aterial[]' does not contain a definition for
Count' and no extension method Count' of type
UnityEngine.$$anonymous$$aterial[]' could be found. Are you missing `System.Linq' using directive?
Any idea on how I can fix this?
The buttons must to be in the same scene as the ball is for this simple script to work.
Change
Count
withLength
(my bad)
Answer by CybernetHacker14 · Oct 05, 2017 at 09:39 AM
I have implemented the System, though it's very complicated requiring two or more scripts.
In the Main Menu scene, I created different rollerballs, then put them inside a gameObject and through script, I stored them in an array of gameObjects. Then set all the elements of the array as SetActive=false. Using GUI buttons and their onClick functionality,firstly I set the element at the current index as SetActive=false, then I change the index of the array, and then set the element at that changed index as SetActive=true. Using a confirm button, I store the value of that index in PlayerPrefs key and in Start(), I refer to that value and use it, so that the next time the main menu scene is loaded, the player will see the rollerball which he has currently selected at start.
In every playable scene(game level) , the rollerball array is created, and using the PlayerPrefs key value which is stored earlier, all the balls except the one that is selected are destroyed. Note that the sequence of those rollerballs in the gameObject array should be the same throughout, otherwise a wrong ball will appear.
CharacterSelectionInMainMenuThis script sets the index of the desired character in Main Menu.
PlayerSelectionInGameLevelThis script gets the index of the selected character, and destroys the other characters, except the one which is selected.
Note that, any other gameObjects working with it, like camera or joysticks, need to be setup in their scripts to detect the player at runtime, you can use FindWithTag or any other method which you find easy to use and understand.
For more references or a tutorial check out this video Character Selection and changing Scene by N3K EN
Answer by AliceWasNeverHere · Oct 05, 2021 at 02:27 AM
Moderators: The replacement of individual characters with this "anonymous" BS is obnoxious AF!
Your answer
Follow this Question
Related Questions
How to make a GUI Texture (image) transparent? 2 Answers
Offset detail texture in c# 1 Answer
Render GUI with Material 1 Answer
How do I get Texture from material 1 Answer
How to create a GUI button to change a character's texture in real-time? 2 Answers