- Home /
Trying to convert a JC script to C #
Hello !
As the title says, I try to convert this script :
 static var statInventory : Inventory;
 
 
 var inventory : Array;
 
 public var emptyTex : Texture;
 public var inventorySizeX = 8;
 public var inventorySizeY = 5;
 var iconWidthHeight = 20;
 var spacing = 2;
 public var offSet = Vector2( 100, 100 );
 private var windowRect : Rect = Rect (20, 20, 200, 400);
 public var itemTitle = "";
 public var itemInfo = "";
 public var infoBox = false;
 var dropZone : Transform;
 
 private var itemMenu = false;
 
 //Our Representation of an InventoryItem
 @System.Serializable
 class InventoryItem
 {
    //GameObject this item refers to
    var worldObject : GameObject;
    //What the item will look like in the inventory
    var texRepresentation : Texture;
 }
 
 // Create the Inventory
 function Awake()
 {
    
     statInventory = this;
     
 
 
 }
 
 
 function Start(){
     inventory = Array(inventorySizeX);
     
     for( var i = 0; i < inventory.length; i ++ ){
     inventory[i] = Array(inventorySizeY);
     inventory.length = inventorySizeX;
     inventory[i].length = inventorySizeY;
     
 }
 }
 
 
 
 
 
 
 
 function Update(){
     
     
     
  if(Input.GetKey (KeyCode.I)){
  iconWidthHeight = 20;
  }
  else{
  iconWidthHeight = 0;
  }
  
  
  
 }
  
  
 
  
  
 
 
 
 
 
 function OnGUI(){
     var currentInventoryItem : InventoryItem;
     var texToUse : Texture;
      
      
 
      //Go through each row
      for( var i = 0; i < inventory.length; i ++ ){
        
          // and each column
          for( var k = 0; k < inventory[i].length; k ++ ){
              currentInventoryItem = inventory[i][k];
              
     
              if( inventory[i][k] == null )
              {
                  GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), emptyTex );
              }
              else
              {
                  GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), currentInventoryItem.texRepresentation );
              }
              
                 if(currentInventoryItem != null && 
                    GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), "", GUIStyle("label") ))
              {
                  
                  currentInventoryItem.worldObject.transform.position = transform.position;
                  currentInventoryItem.worldObject.transform.rotation = transform.rotation;
                  
                  
 
                  
                  
                  if(Input.GetMouseButtonUp(0)){        
                      
                      Debug.Log("Pressé");
                     GUI.Box(Rect(20,10,100,120), "Item Menu");
                     Debug.Log("Marche");
                     if(GUI.Button(Rect(30,40,80,20), "Utiliser")){
                     if(currentInventoryItem.worldObject.GetComponent(InventoryWorldItem).bag == true){
                      var bonusX = currentInventoryItem.worldObject.GetComponent(InventoryWorldItem).xSlots;
                      inventorySizeX = bonusX;
                      var bonusY = currentInventoryItem.worldObject.GetComponent(InventoryWorldItem).ySlots;
                      inventorySizeY = bonusY;
                      Start();
                     Debug.Log("Marche");
                     }
                     }
                     if(GUI.Button(Rect(30,70,80,20), "Jetter")){
                     
                     inventory[i][k] = null;    
                      currentInventoryItem.worldObject.transform.position = dropZone.position;
                      currentInventoryItem.worldObject.transform.parent = null;
                      currentInventoryItem.worldObject.GetComponent(Rigidbody).useGravity = true;
                      currentInventoryItem.worldObject.GetComponent(BoxCollider).enabled = true;
                      currentInventoryItem.worldObject.active = true;
                      currentInventoryItem.worldObject.layer = 0;
                      itemMenu = false;
                      Debug.Log("Marche");
                      }
                      if(GUI.Button(Rect(30,100,5,20), "X")){
                      itemMenu = false;
                      Debug.Log("Marche");
                      }
                 }
 
              }
           
        } 
       
     }
     
 if(infoBox == true){
 
 windowRect = GUI.Window (0, windowRect, WindowFunction, itemTitle);
 }
 
 
 
 
 
 
 }
 
  function WindowFunction (windowID : int) {
 
      GUI.Label (Rect(10, 10, 200, 500), itemInfo);
 }
 
 
 
 function AddItem( item : InventoryItem ){
 
     //Go through each row
     for( var i = 0; i < inventory.length; i ++ )
     {
         // and each column
         for( var k = 0; k < inventory[i].length; k ++ )
         {
            //If the position is empty, add the new item and exit the function
            if( inventory[i][k] == null )
             {
                 item.worldObject.layer = 8;
                
                 inventory[i][k] = item;
                 return;
             }
              if( inventory[i][k] == item){
              
              item.worldObject.layer = 0;
      
              return;
              }
 
         }
        
     } 
     
    
 
  
     
 }
 
 function AddItem( worldObject : GameObject, texRep : Texture ){
 
 
    var newItem = InventoryItem();
    newItem.worldObject = worldObject;
    newItem.texRepresentation = texRep;
    AddItem( newItem ); 
   
 }
I have converted almost everything, but unfortunately, it gives an error at line 10, the array variable (see below) :
C Sharp version :
 using UnityEngine;
 using System.Collections;
 
 public class Inventory : Photon.MonoBehaviour {
 
 
 
 static Inventory statInventory;
 
 string inventory = Array;
 
 public Texture emptyTex;
 public float inventorySizeX= 8;
 public float inventorySizeY= 5;
 public float iconWidthHeight= 20;
 public float spacing= 2;
 public Vector2 offSet = new Vector2( 100, 100 );
 private Rect windowRect = new Rect(20, 20, 200, 400);
 public string itemTitle= "";
 public string itemInfo= "";
 public bool infoBox= false;
 Transform dropZone;
 
 private bool itemMenu= false;
 
 
     
     
 //Our Representation of an InventoryItem
 [System.Serializable]
 class InventoryItem
 {
    //GameObject this item refers to
    GameObject worldObject;
    //What the item will look like in the inventory
    Texture texRepresentation;
 }
 
 // Create the Inventory
 void  Awake (){
    
     statInventory = this;
     
 
 
 }
 
 
 void  Start (){
     inventory = Array(inventorySizeX);
     
     for( i= 0; i < inventory.length; i ++ ){
     inventory[i] = Array(inventorySizeY);
     inventory.length = inventorySizeX;
     inventory[i].length = inventorySizeY;
     
 }
 }
 
 
 
 
 
 
 
 void  Update (){
     
     
     
  if(Input.GetKey (KeyCode.I)){
  iconWidthHeight = 20;
  }
  else{
  iconWidthHeight = 0;
  }
  
  
  
 }
  
  
 
  
  
 
 
 
 
 
 void  OnGUI (){
     InventoryItem currentInventoryItem;
     Texture texToUse;
      
      
 
      //Go through each row
      for( i= 0; i < inventory.length; i ++ ){
        
          // and each column
          for(  k= 0; k < inventory[i].length; k ++ ){
              currentInventoryItem = inventory[i][k];
              
     
              if( inventory[i][k] == null )
              {
                  GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), emptyTex );
              }
              else
              {
                  GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), currentInventoryItem.texRepresentation );
              }
              
                 if(currentInventoryItem != null && 
                    GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), "", GUIStyle("label") ))
              {
                  
                  currentInventoryItem.worldObject.transform.position = transform.position;
                  currentInventoryItem.worldObject.transform.rotation = transform.rotation;
                  
                  
 
                  
                  
                  if(Input.GetMouseButtonUp(0)){        
                      string bonusX;
                     string bonusY;
                      Debug.Log("Pressé");
                     GUI.Box( new Rect(20,10,100,120), "Item Menu");
                     Debug.Log("Marche");
                     if(GUI.Button( new Rect(30,40,80,20), "Utiliser")){
                     if(currentInventoryItem.worldObject.GetComponent<InventoryWorldItem>().bag == true){
                      bonusX= currentInventoryItem.worldObject.GetComponent<InventoryWorldItem>().xSlots;
                      inventorySizeX = bonusX;
                      bonusY= currentInventoryItem.worldObject.GetComponent<InventoryWorldItem>().ySlots;
                      inventorySizeY = bonusY;
                      Start();
                     Debug.Log("Marche");
                     }
                     }
                     if(GUI.Button( new Rect(30,70,80,20), "Jetter")){
                     
                     inventory[i][k] = null;    
                      currentInventoryItem.worldObject.transform.position = dropZone.position;
                      currentInventoryItem.worldObject.transform.parent = null;
                      currentInventoryItem.worldObject.GetComponent<Rigidbody>().useGravity = true;
                      currentInventoryItem.worldObject.GetComponent<BoxCollider>().enabled = true;
                      currentInventoryItem.worldObject.active = true;
                      currentInventoryItem.worldObject.layer = 0;
                      itemMenu = false;
                      Debug.Log("Marche");
                      }
                      if(GUI.Button( new Rect(30,100,5,20), "X")){
                      itemMenu = false;
                      Debug.Log("Marche");
                      }
                 }
 
              }
           
        } 
       
     }
     
 if(infoBox == true){
 
 windowRect = GUI.Window (0, windowRect, WindowFunction, itemTitle);
 }
 
 
 
 
 
 
 }
 
  void  WindowFunction ( int windowID  ){
 
      GUI.Label ( new Rect(10, 10, 200, 500), itemInfo);
 }
 
 
 
 void  AddItem (  InventoryItem item   ){
 
     //Go through each row
     for( i= 0; i < inventory.length; i ++ )
     {
         // and each column
         for( Fk= 0; k < inventory[i].length; k ++ )
         {
            //If the position is empty, add the new item and exit the function
            if( inventory[i][k] == null )
             {
                 item.worldObject.layer = 8;
                
                 inventory[i][k] = item;
                 return;
             }
              if( inventory[i][k] == item){
              
              item.worldObject.layer = 0;
      
              return;
              }
 
         }
        
     } 
     
    
 
  
     
 }
 
 void  AddItem (  GameObject worldObject ,   Texture texRep   ){
 
    string newItem;
    newItem= InventoryItem();
    newItem.worldObject = worldObject;
    newItem.texRepresentation = texRep;
    AddItem( newItem ); 
   
 }
 }
So the error says :
Assets/ScriptCS/Inventory.cs(10,20): error CS0103: The name `Array' does not exist in the current context
Can someone please have a look and try to figure out how to make the array working ?
Thanks for viewing this and sorry for my clumsy english.
Answer by Lachee1 · Jan 19, 2013 at 06:35 AM
C# does not have the Array functions like JScript, instead you must you a List.
It is hard to explain how to use the 'List' type (probably because i suck at explaining), so hopefully this website will help: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
With lists you can also use custom classes.
UPDATE : Solved, I have found the same problem with a complete answer, thanks for your help ! link here
Answer by ElectroSphere · Jan 19, 2013 at 11:44 AM
Thank you, I tried to replace it with :
List inventory = new List();
but now I have this :
Assets/ScriptCS/Inventory.cs(10,1): error CS0246: The type or namespace name
List1' could not be found. Are you missing a using directive or an assembly reference?
What is that "List 1" ? Did I forgot to declare something before or after ?
Your answer
 
 
             Follow this Question
Related Questions
converting javascript to c# 1 Answer
js to c# conversion 1 Answer
Set variable/Array to a length by scripting 2 Answers
How to find the opposite variables list? 1 Answer
Possible to use an array value as the name of a variable? 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                