Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ElectroSphere · Jan 19, 2013 at 06:20 AM · c#javascriptarrayvariableconvert

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ElectroSphere · Jan 19, 2013 at 11:07 PM 0
Share

UPDATE : Solved, I have found the same problem with a complete answer, thanks for your help ! link here

avatar image
0

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 ?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges