Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

[SOLVED]Using an elastic script

Discussion in 'Immediate Mode GUI (IMGUI)' started by primoadamg, Sep 14, 2009.

  1. primoadamg

    primoadamg

    Joined:
    Aug 17, 2009
    Posts:
    98
    elastic, what I mean is I want a script that can be used on multiple objects with different variables for example lets say you pick up an item and it says

    something like this
    Item1
    garbage
    9999

    and another like this
    Item2
    Trash
    1 1/2

    is it possible to take a single script and use it on multiple game Objects and still be able differentiate between Item1 and Item2??? this just would take a whole lota weight off my shoulders
     
  2. runningbird

    runningbird

    Joined:
    Sep 3, 2009
    Posts:
    382
    just make sure in your script you have


    Code (csharp):
    1.  
    2. var itemID : string;
    3. var description : string;
    4. var amount : float;
    5.  
    Then you can drag the script to each object in the game and it will use the variables but you can set them for each object.
     
  3. primoadamg

    primoadamg

    Joined:
    Aug 17, 2009
    Posts:
    98
    But if I have two Items using the same script trying to display the info of the script then how to I differentiate script from script if I use
    (Inventory.js)
    Code (csharp):
    1.  
    2. var ItemScript : Item;//Item is the script Item.js
    3.  
    4. var Name = ItemScript.Name;
    5.  
    6. blah blah
    7.  
    (Item.js)
    Code (csharp):
    1.  
    2. var Name = "Item1";
    3.  
    (Item.js)another item
    Code (csharp):
    1.  
    2. var Name = "Item2"
    3.  
    so if I want it to display the name it has either "Item1" or "Item2" to use...
     
  4. runningbird

    runningbird

    Joined:
    Sep 3, 2009
    Posts:
    382
    In the inspector window you will see the variables for your script and per object you can set them to different variables. That way you can reuse the code in the script but give each object specific variables.
     
  5. primoadamg

    primoadamg

    Joined:
    Aug 17, 2009
    Posts:
    98
    Okay, so other scripts trying to access that script wont get it confused with diffrent gameObjects... thats what I was looking for.
    Thanks