Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change material script

Discussion in 'Scripting' started by AaronC, Jul 28, 2006.

  1. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Hey guys, I'm just trying a mod of the change materials script from the script examples download. Is there some thing wrong ? Im getting an error message right at the end, and only the first 21 varibles are being exposed

    Ive just got a cube and 30 stills, 256x256. Ive added a *1,*2,*3 etc repeatedly.Have set materials to duffuse, fast
    thanks
    AC
    PS I just finished the scripting tutorial(Until I checked it out, I thought it was the rasing-scripting tut-which I will do next)
    Thanks whoever did the scripting tutorial, I found it fun and useful
    Code (csharp):
    1. // Anything declared with var outside a function is visible and can be changed in the inspector
    2. var material1 : Material;
    3. var material2 : Material;
    4. var material3 : Material;
    5. var material4 : Material;
    6. var material5 : Material;
    7. var material6 : Material;
    8. var material7 : Material;
    9. var material8 : Material;
    10. var material9 : Material;
    11. var material10 : Material;
    12. var material11 : Material;
    13. var material12 : Material;
    14. var material13 : Material;
    15. var material14 : Material;
    16. var material15 : Material;
    17. var material16 : Material;
    18. var material17 : Material;
    19. var material18 : Material;
    20. var material19 : Material;
    21. var material20 : Material;
    22. var material21 : Material;
    23. var material22 : Material;
    24. var material23 : Material;
    25. var material24 : Material;
    26. var material25 : Material;
    27. var material26 : Material;
    28. var material27 : Material;
    29. var material28 : Material;
    30. var material29 : Material;
    31. var material30 : Material;
    32.  
    33. function Start () {
    34.     // time shall repeat in the range 0 ... 1
    35.     time = Mathf (Time.time, 1);
    36.     if (time < 1*0.33) {
    37.         renderer.sharedMaterial = material1;
    38.     }
    39.     else
    40.     {if (time < 2*0.33) {
    41.         renderer.sharedMaterial = material2;
    42.     }
    43.     else
    44.     {if (time < 3*0.33) {
    45.         renderer.sharedMaterial = material3;
    46.     }
    47.     else
    48.     {if (time < 4*0.33) {
    49.         renderer.sharedMaterial = material4;
    50.     }
    51.     else
    52.     {if (time < 5*0.33) {
    53.         renderer.sharedMaterial = material5;
    54.     }
    55.     else
    56.     {if (time < 6*0.33) {
    57.         renderer.sharedMaterial = material6;
    58.     }
    59.     else
    60.     {if (time < 7*0.33) {
    61.         renderer.sharedMaterial = material7;
    62.     }
    63.     else
    64.     {if (time < 8*0.33) {
    65.         renderer.sharedMaterial = material8;
    66.     }
    67.     else
    68.     {if (time < 9*0.33) {
    69.         renderer.sharedMaterial = material9;
    70.     }
    71.     else
    72.     {if (time < 10*0.33) {
    73.         renderer.sharedMaterial = material10;
    74.     }
    75.     else
    76.     {if (time < 11*0.33) {
    77.         renderer.sharedMaterial = material11;
    78.     }
    79.     else
    80.     {if (time < 12*0.33) {
    81.         renderer.sharedMaterial = material12;
    82.     }
    83.     else
    84.     {if (time < 13*0.33) {
    85.         renderer.sharedMaterial = material13;
    86.     }
    87.     else
    88.     {if (time < 14*0.33) {
    89.         renderer.sharedMaterial = material14;
    90.     }
    91.     else
    92.     {if (time < 15*0.33) {
    93.         renderer.sharedMaterial = material15;
    94.     }
    95.     else
    96.     {if (time < 16*0.33) {
    97.         renderer.sharedMaterial = material16;
    98.     }
    99.     else
    100.     {if (time < 17*0.33) {
    101.         renderer.sharedMaterial = material17;
    102.     }
    103.     else
    104.     {if (time < 18*0.33) {
    105.         renderer.sharedMaterial = material18;
    106.     }
    107.     else
    108.     {if (time < 19*0.33) {
    109.         renderer.sharedMaterial = material19;
    110.     }
    111.     else
    112.     {if (time < 20*0.33) {
    113.         renderer.sharedMaterial = material20;
    114.     }
    115.     else
    116.     {if (time < 21*0.33) {
    117.         renderer.sharedMaterial = material21;
    118.     }
    119.     else
    120.     {if (time < 22*0.33) {
    121.         renderer.sharedMaterial = material22;
    122.     }
    123.    
    124.     else
    125.     {if (time < 23*0.33) {
    126.         renderer.sharedMaterial = material23;
    127.     }
    128.     else
    129.     {if (time < 24*0.33) {
    130.         renderer.sharedMaterial = material24;
    131.     }
    132.     else
    133.     {if (time <25*0.33) {
    134.         renderer.sharedMaterial = material25;
    135.     }
    136.     else
    137.     {if (time < 26*0.33) {
    138.         renderer.sharedMaterial = material26;
    139.     }
    140. else
    141.     {if (time < 27*0.33) {
    142.         renderer.sharedMaterial = material27;
    143.     }
    144.     else
    145.     {if (time < 28*0.33) {
    146.         renderer.sharedMaterial = material28;
    147.     }
    148.     else
    149.     {if (time < 29*0.33) {
    150.         renderer.sharedMaterial = material29;
    151.     }
    152.     else
    153.     {if (time < 30*0.33) {
    154.         renderer.sharedMaterial = material30;
    155.     }
    156.    
    157.     }
    158. }
    error says"expecting},found"
    \\there are no quotemarks on the line giving errors. I dont mind if its a bug but its flaring up my console something wicked
     
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Heres the package if anyone feels like helping...
    EDIT
    Problem solved package removed
    Thanks everybody!
    AC
     
  3. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216
    Damn your code is hard to read.... Your braces don't line up with how you've tabbed things. I'd recommend you use a different method of formatting such as:
    Code (csharp):
    1. if (true) {
    2.    if (true) {
    3.       // do something
    4.    } else {
    5.       // this won't happen
    6.    }
    7. } else {
    8.    // do something else
    9. }
    That way it's obvious what braces close which. If you don't like how that looks, the other way is:
    Code (csharp):
    1. if (true)
    2. {
    3.    if (true)
    4.    {
    5.       // do something
    6.    }
    7.    else
    8.    {
    9.       // this won't happen
    10.    }
    11. }
    12. else
    13. {
    14.    // do something else
    15. }
     
  4. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Yeah sorry, Im quite new to this. People here are real helpful though. Thanks for the tip. I hunted round the library but there is nothing on javascript outside of web stuff, and I dont wanna get bogged down with irrelevant stuff. Im just about there, following the tut's and references
    Oh and the code above is slightly different to the one in the package :roll:
    AC
     
  5. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216
    Sorry to double post, but I thought I should show what your code is doing.

    Here's what your code looks like if you reformat it:
    Code (csharp):
    1. ...
    2. function Start () {
    3.    // time shall repeat in the range 0 ... 1
    4.    time = Mathf (Time.time, 1);
    5.    if (time < 1*0.33) {
    6.       renderer.sharedMaterial = material1;
    7.    } else {
    8.       if (time < 2*0.33) {
    9.          renderer.sharedMaterial = material2;
    10.       } else {
    11.          if (time < 3*0.33) {
    12.             renderer.sharedMaterial = material3;
    13.          } else {
    14.             if (time < 4*0.33) {
    15.                renderer.sharedMaterial = material4;
    16.             } else {
    17.                if (time < 5*0.33) {
    18.                   renderer.sharedMaterial = material5;
    19.                } else {
    20.                   if (time < 6*0.33) {
    21.                      renderer.sharedMaterial = material6;
    22.                   } else {
    23.                      if (time < 7*0.33) {
    24.                         renderer.sharedMaterial = material7;
    25.                      } else {
    26. ...
    You can see where this is going. I suspect this isn't the functionality that you had planned on implementing. Basically, all you need to do is remove the opening brace { after each else statement so you have else if statements. Alternatively, you might want to look into the switch statement.
     
  6. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Other than that, using an array for the materials (similar to this) will make the code much shorter. Like this:
    Code (csharp):
    1.  
    2. var materials : Material[];
    3. var changeInterval = 0.33;
    4.  
    5. function Update()
    6. {
    7.     // if no materials are setup, do nothing (to prevent division by zero)
    8.     if( materials.length == 0 )
    9.         return;
    10.  
    11.     // we want this material index now
    12.     var index : int = Time.time / changeInterval;
    13.     // take a modulo with materials size so that animation repeats
    14.     index = index % materials.length;
    15.     // assign it
    16.     renderer.sharedMaterial = materials[index];
    17. }
    18.  
    I've done a couple of other changes: first you want the code in Update function, because Start will be called only once. Then I made animation speed a tweakable property in the inspector.
     
  7. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Brilliant guys!Thanks again!
    (will remove package once I have it nailed)
    AC
     
  8. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I'd suggest googling for something like "javascript basics" or "javascript tutorials" and going through some of the sites you find that way. Just skip the obviously web-related material like popups and whatnot. The basics are still the same, and you definitely want to know how to use the correct syntax and useful things like arrays.

    I had to do that myself...even though I've been programming off and on for over 20 years, I've never used Javascript before, so I'm still learning some of the syntax. (I know you can also use Boo, but I've never used that either, and I hate anything related to C, sorry! Now if Unity used perl I'd be all set. ;) )

    Edit: Just read Jeremy's post and followed the link. So now I'll offer a bit more advice, if you don't mind.

    I'm afraid there's no way around that. You HAVE to. That's the only way to get Unity to do cool stuff. If they made it "user friendly" like, say, Automator, you'd be severely limited in the things you can do. It would end up like all those other "user friendly" game engines, where you're stuck making minor variations on platformers or shoot'em'ups or whatever. Instead, you can make ANYTHING. That freedom does have a bit of a price.

    Unity is amazingly user friendly for what it does. Considering that someone like me, whose last game (that I sold) was programmed in assembly language on the Commodore 64 back in the late 80s, has some chance to actually make a modern 3D game that looks cool and runs fast...well, that's just truly astonishing. Without Unity, there's no hope that I'd be able to learn the OpenGL code and all the rest of it, at least not without years and years of catching up. Programming the occasional general purpose app in perl and so on doesn't really cut it as far as making games go, though at least it's kept the programming neurons in my brain from completely collapsing. ;)

    I'd suggest sticking with it, and starting with really small and silly things rather than trying to leap into a Big Project all at once. It will probably take a while before you can make anything substantial, but that's true for everybody.

    There's a little bit of a trap with Unity...it makes things seem so effortless that you want the game logic to be the same way, but until OTEE implements a ReadMyMind function, or maybe a DoWhatIWantNotWhatITellYou function, that part is always going to be pretty involved. Any worthwhile game is still going to take a lot of time and effort, but without Unity that would be (time + effort) * 100. I mean, look at modern AAA games...team sizes of dozens or hundreds of people working for literally years, and despite that you seem to increasingly end up with games that take just a few hours to get through. (Hence the focus on multiplayer?)

    Give yourself at least a few weeks to learn Javascript enough that you're comfortable with it, and I think you'll find that things become easier and more understandable, and you'll save yourself a lot of headaches later.

    OK, enough blathering from me. :)

    --Eric
     
  10. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    A couple JavaScript resources:

    http://www.w3schools.com/js/default.asp

    http://wp.netscape.com/eng/mozilla/3.0/handbook/javascript/

    The beauty of Unity isn't that it lets you avoid programming, it's that it lets you do programming that gets so much DONE so easily and effectively.

    I don't think Unity is bad way to learn programming though--just start with small ideas and see what you can figure out. It's fun! And when you get comfortable, tackle more complex projects.

    (It would be is someone made a beginner programming lesson series--for non-programmers--that really covered JS well, but from a Unity perspective. It would cover basic programming concepts, and JS in depth, but would skip the Web stuff, and would also skip most Unity-specific stuff. It would introduce some Unity stuff of necessity, but the focus would be on JavaScript as an into to programming concepts. Then people could move from there to the existing Unity tutorials.)
     
  11. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Unity needs a "cookbook".
     
  12. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    The community is trying to accomplish that with the script wiki. If it's going to succeed in the long term people have got to keep contributing to it, though, and improving what's there.

    -Jon
     
  13. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Eric And Jeremy-
    Yes I value what you have to say-an important distinction I and other novices have to make is between Java and Javascript. Im getting it...With a slab of help from the good Unity People and yourselves...And I have found a 60kg book with a paragraph of relevant stuff...
    For others:
    All I did here was add Aras's script, And FreyR's bottom script from the link Aras provided...

    Thanks crew, Thread out
    AC
     
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's a start. ;) Keep at it...you'll get it and then it will become very rewarding. (Expect the occasional roadblock though...I wasted a full hour today because my script had += when I meant +. Despite going through the usual debugging stuff I could not for the life of me figure out what was wrong, and I looked at that line 50 million times without seeing the obvious problem...possibly because I had a += elsewhere that I actually did need. I was just seeing what I wanted to see. Duh! :) )

    --Eric
     
  15. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Go team, Targos. The work is well worth it IMO... But I like programming so... ;-)

    I think we have all had those days when we spend a long time looking for a real simple error. Sometimes I find the best thing to do when you are getting nowhere is to go do something else non-computer. Get something to eat or drink, then come back with fresh eyes and look again. A lot of times the bug will jump right off the screen and you will be reapeating "idiot, idiot, idiot" in your head. :)

    -Jeremy