Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Need Help For Mini Script ( Change Object Material )

Discussion in 'Scripting' started by MR.Dawoodi, Dec 11, 2010.

  1. MR.Dawoodi

    MR.Dawoodi

    Joined:
    Dec 11, 2010
    Posts:
    22
    hi guys!
    i'm an environment artist and , i dont know anything about script :( :confused:
    i need help for script when i click on object , object will change with list of objects,Respectively.
    And an example for similar material change.

    thank's :rolleyes:
     
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Do you mean that when the object is clicked on, it should be replaced by the next object in a list of objects?
     
  3. MR.Dawoodi

    MR.Dawoodi

    Joined:
    Dec 11, 2010
    Posts:
    22
    yes !
    select one and replace with it.

    sry about my english :rolleyes:
     
  4. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    For clicking on an object, give the object a collider and use the OnMouseDown() function. As for replacing the object with a new one, I answered a similar question here.
     
  5. MR.Dawoodi

    MR.Dawoodi

    Joined:
    Dec 11, 2010
    Posts:
    22
    thank Jesse for your helpfull !
    but i don't know anything about scripting :(
    can u write a mini script for this ?

    sry for this request :(
     
  6. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    You'll have to learn some programming if you want to be able to use Unity effectively. There are plenty of tutorials and references available though that you can use to get started (just search the forums for 'scripting tutorial', and you should find some).

    Meanwhile, here's what the code might look like in C# (not compiled or tested):

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class ReplaceOnClick : MonoBehaviour
    4. {
    5.     public GameObject prefab;
    6.  
    7.     void OnMouseDown()
    8.     {
    9.         GameObject.Instantiate(prefab, transform.position, transform.rotation);
    10.         Destroy(gameObject);
    11.     }
    12. }
    (That's off the top of my head, so I might not have gotten all the details right.)
     
  7. MR.Dawoodi

    MR.Dawoodi

    Joined:
    Dec 11, 2010
    Posts:
    22
    thank's Jesse.
    i test it , it work ;)
    but ,If I want to act as a rotating between several object what should I do?
     
  8. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Using the above script (or one like it), you could create a set of prefabs, each of which references another prefab in the set. For example, for three prefabs A, B, and C, A would reference B, B would reference C, and C would reference A. Then, clicking on the objects would result in cycling through the object types.

    (There are other options, but that's probably the most straightforward solution.)
     
  9. MR.Dawoodi

    MR.Dawoodi

    Joined:
    Dec 11, 2010
    Posts:
    22
    thank's jesse.
    i use it , it work with several object ;)
    you are my script hero :D