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 Aljoscha · May 15, 2014 at 11:15 AM · guibuttonmaterialchangeswitch

How can I combine GUI button with script that is put on different gam objects?

My problem is: I have written a C# script which allows me to switch between different materials. The materials are used on several different game objects. When I control the switching by the function: if (Input.GetKeyDown(KeyCode.K)) everything works fine. All the game objects change their material. Now, when I want to use a GUI button: if (GUI.Button (new Rect (10,10,150,100), "button")) to control the switching of the materials it works only on 1 game object not on all as it is desired. Can anybody tell me why this happens or how I can manage this problem???

Thanx to everyone!!!

Aljoscha

using UnityEngine; using System.Collections;

public class SWP2 : MonoBehaviour {

 public Material[] mats;
 public GameObject ga;
 private int index = 0;
 
 // Use this for initialization
 void Start () {
     ga.renderer.material = mats[index];
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown(KeyCode.K))
     {
         index++;
         if (index > mats.Length - 1)
         {
             index = 0;
         }
         ga.renderer.material = mats[index];
     }
 }

}

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 robertbu · May 15, 2014 at 01:35 PM 0
Share

There are a number approaches, but to recommend one, I need to understand more about what you are trying to do:

  • How is your code currently structured so that this works? Do you have this script on multiple objects?

  • Are the materials (mat) the same for all the objects?

  • Do all the objects that need to change share the same tag?

  • How are the materials different?

  • Do all the objects you want to change start out with the same material?

avatar image Aljoscha · May 15, 2014 at 01:53 PM 0
Share

Hello robertbu,

1) yes I have the script on many game objects. (all in all I have 1 Objects which is made from many game objects) 2) yes the materials are the same for all the objects. I just want to switch between 2 textures and therefore I tried to do this by switching 2 materials. 3) I don't know exactly about tags. sorry I'm a beginner. 4) different Textures only. 5) YES they do.

Thanks for your help and advice!!!

greetz

Aljoscha

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · May 15, 2014 at 02:21 PM

For what you outline a simple solution is to change the texture of the sharedMaterial.

  • Attach the following to script to an empty game object

  • Drag an drop any one of the game objects that you want to change to the 'ga' variable

  • Set the size of the texs, and drag and drop textures into the slots

  • Hit play

Note that when you stop the app, you will see the last material displayed. That is, when run in the editor, it makes a permanent change to the material. That is why the first texture is assigned in Start(). This will work fine in a build.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour {
 
     public GameObject ga;
     public Texture[] texs;
     private int index = 0;
 
     void Start() {
         ga.renderer.sharedMaterial.mainTexture = texs[0];
     }
 
     void OnGUI() {
         if (GUI.Button (new Rect(10,10, 75, 35), "Change")) {
             index = (index + 1) % texs.Length;
             ga.renderer.sharedMaterial.mainTexture = texs[index];
         }
     }
 }

Note that there other approaches more along the lines of what you were seeking to do. So if this does not work for you, I'll be glad to outline an alternate solution.

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

20 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

Related Questions

Change GUI button position in code 1 Answer

Change Text of GUI Button from Script 2 Answers

Change Materials On Button Press or Hold? 1 Answer

How to create a GUI button to change a character's texture in real-time? 2 Answers

Changing an objects material by clicking GUI button 0 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