Search Unity

Invo Problem (with error)

Discussion in 'Immediate Mode GUI (IMGUI)' started by legand, Jan 23, 2011.

  1. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    Okay so i added Angels invo and i am getting two errors shown below

    InvalidCastExeption: cannot cast from source type
    inv2.GUI() (at Assets/_scripts/inv2.js:59)

    I have (3) of those errors it says all (3) are the SAME errors exact same

    Thanks you and have a good day i help to get help soon :)
     
  2. AzulValium

    AzulValium

    Joined:
    Sep 14, 2009
    Posts:
    103
    Post the script too !
     
  3. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    Okay

    Inv2 Script

    Code (csharp):
    1.  
    2.  
    3. //Èíâåíòàðü
    4. var inventory : Array;
    5.  
    6. var doWindow : boolean = false;
    7.  
    8. //Êàðòèíêà ïóñòîãî ñëîòà
    9. public var emptyTex : Texture;
    10.  
    11. //Ðàçìåð èíâåíòàðÿ (êîë-âî ÿ÷ååê)
    12. public var inventorySizeX = 4;
    13. public var inventorySizeY = 5;
    14.  
    15. //Ðàçìåð ÿ÷åéêè ïî âûñîòå è øèðèíå
    16. var iconWidthHeight = 40;
    17.  
    18. //Ïðîñòðàíñòâî ìåæäó ñëîòàìè (â X è Y)
    19. var spacing = 4;
    20.  
    21. //Ïîçèöèÿ èíâåíòàðÿ
    22. public var offSet = Vector2( 100, 100 );
    23.  
    24.  
    25. // Íàçíà÷àþòñÿ ïî ÷åë÷êàì êíîïîê ìûøêè (ñì. Update())
    26. private var itemImage : Texture2D;
    27.  
    28.  
    29. //êëàññ è ïðåäìåòàõ èíâåíòàðÿ
    30. class InventoryItem
    31. {
    32.    //Ýòîò ïóíêò îòíîñèòñÿ ê Ãåéìîáúåêòó
    33.    var worldObject : GameObject;
    34.    //Òî, êàê ìû áóäåì âèäåòü åãî â èíâåíòàðå
    35.    var texRepresentation : Texture2D;
    36. }
    37.  
    38. // Ñîçäàåì èíâåíòàðü
    39. function Awake()
    40. {
    41.    inventory = new Array(inventorySizeX);
    42.    
    43.     for( var i = 0; i < inventory.length; i ++ )
    44.     {
    45.         inventory[i] = new Array(inventorySizeY);
    46.     }
    47. }
    48.  
    49.  
    50. function OnGUI() {
    51.  
    52.    var texToUse : Texture2D;
    53.    var currentInventoryItem : InventoryItem;
    54.  
    55.     //Ïåðåõîä ÷åðåç êàæäóþ ñòðîêó
    56.     for( var i = 0; i < inventory.length; i ++ )
    57.     {
    58.         // è êàæäûé ñòîëáåö
    59.         for( var k = 0; k < inventory[i].length; k ++ )
    60.         {
    61.            texToUse = emptyTex;
    62.            currentInventoryItem = inventory[i][k];
    63.            
    64.             //Åñëè åñòü ïóíêò â I-é ñòðîêè è K-ãî ñòîëáöà, íàðèñîâàòü
    65.             if( inventory[i][k] != null )
    66.             {
    67.                 texToUse = currentInventoryItem.texRepresentation;
    68.             }
    69.            
    70.             var it = GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
    71.             GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
    72.             var w : int = 0;
    73.             var h : int = 0;
    74.             if (it){// (inventory[i][k]==!null){
    75.  
    76.             //òóò áóäåò äðîï
    77.             Debug.Log("YES!!!!!!!!!!!!!!!!!!!");
    78.             }
    79.         }
    80.     }
    81. }
    82.  
    83. function AddItem( item : InventoryItem )
    84. {
    85.     //Ïðîõîä ïî êàæäîé ñòðîêå
    86.     for( var i = 0; i < inventory.length; i ++ )
    87.     {
    88.         // è êàæäîìó ñòîëáöó
    89.         for( var k = 0; k < inventory[i].length; k ++ )
    90.         {
    91.            //Åñëè ïîçèöèÿ ïóñòàÿ, äîáàâèòü íîâûé ïóíêò è âûõîä èç ôóíêöèè
    92.            if( inventory[i][k] == null )
    93.             {
    94.                 inventory[i][k] = item;
    95.                 return;
    96.             }
    97.         }
    98.     }  
    99.    
    100.     //Åñëè èíâåíòàðü ïîëîí, òóò ÷òî-íèòü äåëàåì  
    101. }
    102.  
    103. function AddItem( worldObject : GameObject, texRep : Texture2D )
    104. {
    105.    var newItem = new InventoryItem();
    106.  
    107.     newItem.worldObject = worldObject;
    108.     newItem.texRepresentation = texRep;
    109.      
    110.    AddItem( newItem );  
    111. }
    112.  
    113. [END CODE][/COLOR][/COLOR]
    114.  
    115. [COLOR="navy"]
    116. Item Script
    117.  
    118. [COLOR="blue"][CODE]
    119.  
    120. public var InventoryIcon :  Texture2D;
    121. public var ItemDescription : String;
    122. public var ItemClass : String;
    123. public var defence : int;
    124. public var damage : int;
    125.  
    126. var doWindow : boolean = false;
    127.  
    128. function OnMouseOver() {
    129. doWindow=true;
    130. }
    131. function OnMouseExit() {
    132. doWindow=false;
    133. }
    134.  
    135. function OnGUI() {
    136.  
    137. if (doWindow)
    138. GUI.Window (0, Rect (110,10,200,80), DoWindow, "Info");
    139. }
    140.  
    141. function OnMouseDown() {
    142.     var inv = FindObjectOfType(inv2);
    143.     inv.AddItem( gameObject, InventoryIcon );
    144.     Destroy(this.gameObject);
    145.     Debug.Log("item picked");
    146. }
    147.  
    148.     function DoWindow (windowID : int) {
    149.     GUI.Label (new Rect(5, 15, 200, 25), "Item:" + ItemDescription);
    150.     GUI.Label (new Rect(5, 30, 90, 25), "Class:" + ItemClass);
    151.     GUI.Label (new Rect(5, 45, 90, 25), "Defence:" + defence);
    152.     GUI.Label (new Rect(5, 60, 90, 25), "Damage:" + damage);
    153.     }
    154.    
    155.  
    156. [END CODE][/COLOR][/COLOR]
    157.  
    158.  
    159. [COLOR="magenta"]DraggableGUIElement script
    160.  
    161. [CODE]
    162.  
    163. using UnityEngine;
    164.  
    165. using System.Collections;
    166.  
    167. public class DraggableGUIElement : MonoBehaviour
    168. {
    169.     [System.Serializable]
    170.     public class Border
    171.     {
    172.         public float minX, maxX, minY, maxY;
    173.     }
    174.  
    175.     public Border border;
    176.  
    177.     Vector3 lastMousePosition;
    178.  
    179.     void OnMouseDown()
    180.     {
    181.         lastMousePosition = GetClampedMousePosition();
    182.     }
    183.  
    184.     Vector3 GetClampedMousePosition()
    185.     {
    186.         Vector3 mousePosition = Input.mousePosition;
    187.         mousePosition.x = Mathf.Clamp(mousePosition.x, 0f, Screen.width);
    188.         mousePosition.y = Mathf.Clamp(mousePosition.y, 0f, Screen.height);
    189.  
    190.         return mousePosition;
    191.     }
    192.  
    193.     void OnMouseDrag()
    194.     {
    195.         Vector3 delta = GetClampedMousePosition() - lastMousePosition;
    196.  
    197.         delta = Camera.main.ScreenToViewportPoint(delta);
    198.  
    199.         transform.position += delta;
    200.  
    201.         Vector3 position = transform.position;
    202.         position.x = Mathf.Clamp(position.x, border.minX, border.maxX);
    203.         position.y = Mathf.Clamp(position.y, border.minY, border.maxY);
    204.  
    205.         transform.position = position;
    206.  
    207.         lastMousePosition = GetClampedMousePosition();
    208.     }
    209. }
    210.  
    211. [END CODE][/COLOR]
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Legand - sorry for the reply to the PM. It was a little terse. Recently, I have had a few people that have been asking for help and were not listening to my suggestions or solutions. I answered that PM on my iPhone and misunderstood who it was from. This system works, (tho' admittedly it's a bit stale), but it does need to added into your game's system by you. If I really get enough interest, I can put it up higher on my priority list to get something that can be put up on the asset store and is more useable and standalone.

    Take a look at this thread here on the Unity3D forums:
    http://forum.unity3d.com/threads/49202

    And this thread on my site:
    http://theantranch.com/Unity/Entries/2010/5/17_Basic_Inventory_&_Looting_System.html


    If you get this:

    This is the answer:
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Frankly, I should fix that line of code for 3.0, or at least mention is on the site... I'll try to get to it Monday.
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    PPS: Code tags on this forum are [ code ] and [ / code ] without the spaces. Or the # icon in the advanced posting page.

    What text editor are you using? I'm curious about all of the:
    //Åñëè åñòü ïóíêò â I-é ñòðîêè è K-ãî ñòîëáöà, íàðèñîâàòü

    Are these your notes in non-roman characters?
     
  7. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    oh okay :) btw its still saying it and i jhave No club at all why it is lol
     
  8. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    I am just opening it in Unitsite or whatever you call it
     
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Are you renaming anything?

    I should have no elements names Inv2:


    Could you be mixing two sets of code by accident?

    I'll send you a new updated package tomorrow or Tuesday with the correction to the casting error:
    Code (csharp):
    1. lootTable = FindObjectOfType (LootTable) as LootTable;  //  Create a reference to the Loot Table
    Or you can correct the lines that error out with the above line of code.
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'll also have to admit that I'm not looking incredibly close at your errors, as the only casting errors in my Unity-iPhone 1.7 code was this line:
    lootTable = FindObjectOfType (LootTable);

    ... which in 3.x, oddly, needed to be cast to a type with:
    lootTable = FindObjectOfType (LootTable) as LootTable;

    Your case of:
    InvalidCastExeption: cannot cast from source type
    inv2.GUI() (at Assets/_scripts/inv2.js:59)


    ... doesn't make sense in the context of my code.

    There must be some other issue going on.

    Could you be mixing two sets of code by accident? Or have you re-written or renamed anything?
     
  11. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    Give me the link to RE download it please something must have happend
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    This page:
    http://theantranch.com/Unity/Entries/2010/5/17_Basic_Inventory_&_Looting_System.html

    Has this link:
    http://theantranch.com/tutorialsupport/downloads/InventoryAndLoot/InventoryAndLootSystem.zip

    And you must change this line of code in LootableObject.js:
    Line 23
    Code (csharp):
    1.     lootTable = FindObjectOfType (LootTable);   //  Create a reference to the Loot Table
    to:
    Line 23
    Code (csharp):
    1.     lootTable = FindObjectOfType (LootTable) as LootTable;  //  Create a reference to the Loot Table
    This should work.
     
  13. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    kk ill try it
     
  14. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    the package wont import...
     
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Try these steps:


    The scripts should also be importable on their own... You can ignore the graphics, models, textures and materials.
     
  16. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    like i said it WONT let me import it it makes a Ping sound and no windows open nothing...
     
  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Dunno then... You got me! That was a fresh download from the site. And it imported fine and worked as expected.

    The new version 2.0 will be available on the asset store soon. There will be free textures, models and materials to go along with the project. There will be comprehensive tutorial videos as well. That might help.
     
  18. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    isn't that the one you were gonna have me beta test?
     
  19. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Yes, but if you can't import this package, you won't be able to import the beta.
     
  20. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    Okay i got it imported and when press I a invo window opens but not will like a grid like thing and when i dragged a chest onto the screen and click on it it dont loot it :(
     
  21. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    Okay i got it so when i press i it opens and it has the grid look for were items go but now when i put a item on the ground its a rectangle and when i click it it doesnt do anything and how do i open my equip thing
     
  22. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Are you putting "lootable object" scripts on your objects? They won't be lootable without them.

    Read all the content of both these threads:

    The open the demo project and look how it's set up and look at how the chests are set up for looting.

    -

    Remember 2.0 is just around the corner and it behaves a little differently, especially setting up loot tables and lootable objects.
     
  23. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    Okay by the way want me to text out the demo since i got it all working?>??
     
  24. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    When i put the chest armor on the scene and then i put the loot object on it then when i go to play i cant move
     
  25. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    Well... idk now
     
  26. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    hmmm no one????
     
  27. legand

    legand

    Joined:
    Jan 22, 2011
    Posts:
    371
    Okayyy then...