Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Ziboo · Jul 18, 2011 at 11:27 AM · c#javascripttranslate

Problem Javascript to C#

Hi everyone,

I'm trying to rewrite my script I made in JavaScript to C#. I have some difficulty doing some things:

How can I do the following in C# :

Wait for a function to completely finish:

 function MyFunction01()
 {
 ...
 ...
 return true;
 }
 
 function MyFunction02()
 {
 yield MyFunction01();
 Debug.Log("Finished");
 }

Pass a function as parameters:

 function getNewID()
 {
 var newID = ...;
 ...
 return newID;
 }
 
 function myFunction(x : int, y : int, z : int)
 {
 ...
 }
 
 myFunction(getNewID(),getNewID(),getNewID())


And here the last thing, but not least, very strang, giving me a null reference when trying to acces the Tile variable, it seams that in C# I can't use the Tile Variable between the if/ else if:

JavaScript (Working)

 function myFunction()
 {
     var WallTiles : Object[] = Resources.LoadAll("Dungeon/Walls");
     for(var y=0;y<generator.ysize;y++)
     {
         for(var x=0;x<generator.xsize;x++)
         {
             var TheCell : int = generator.getCell(x,y);
 
             if(TheCell == 1)
             {
                 var typeOfTile : Array = generator.getDirectionArray(x,y);
                 if(typeOfTile[0] == 0) Tile = Instantiate(WallTiles[Random.Range(0,WallTiles.Length)],Vector3(x*tileSize,0,-y*tileSize),Quaternion.identity); //WALl
                 else if(typeOfTile[0] == 1) Tile = Instantiate(CornerTiles[Random.Range(0,CornerTiles.Length)],Vector3(x*tileSize,0,-y*tileSize),Quaternion.identity); //CORNER
                 else if(typeOfTile[0] == 2) Tile = Instantiate(DoubleCornerTiles[Random.Range(0,DoubleCornerTiles.Length)],Vector3(x*tileSize,0,-y*tileSize),Quaternion.identity); //DOUBLE CORNER
                 Tile.transform.parent = dungeonParent.transform;
                 Tile.transform.Rotate(Vector3(0,90 * typeOfTile[1],0));
 
                 //Create Objects
                 if(typeOfTile[0] == 0) PopulateObject(Tile, x , y);
             }
         }
     }
 }
     
 C#
 
 private void myFunction()
 {
     Object[] WallTiles = Resources.LoadAll("Dungeon/Floors");
     for(int y=0;y<generator.ysize;y++)
     {
         for(int x=0;x<generator.xsize;x++)
         {
 
             int TheCell = generator.getCell(x,y);
 
             GameObject Tile;
             
             //Create Walls
             if(TheCell == 1)
             {
                 int[] typeOfTile = generator.getDirectionArray(x,y); 
                 if(typeOfTile[0] == 0)  Tile = Instantiate(WallTiles[Random.Range(0,WallTiles.Length)],new Vector3(x*tileSize,0,-y*tileSize),Quaternion.identity) as GameObject; //WALl
                 
                 else if(typeOfTile[0] == 1)  Tile = Instantiate(CornerTiles[Random.Range(0,CornerTiles.Length)],new Vector3(x*tileSize,0,-y*tileSize),Quaternion.identity)  as GameObject ; //CORNER
                 
                 else if(typeOfTile[0] == 2)  Tile = Instantiate(DoubleCornerTiles[Random.Range(0,DoubleCornerTiles.Length)],new Vector3(x*tileSize,0,-y*tileSize),Quaternion.identity)  as GameObject ; //DOUBLE CORNER    
 
                 Tile.transform.parent = dungeonParent.transform;
                 Tile.transform.Rotate(new Vector3(0,90 * typeOfTile[1],0));
                 
                 if(typeOfTile[0] == 0) PopulateObject(Tile as GameObject, x , y)
             }
         }
     }
 }


Thank you very much if you take the time to answer me, I'm kind of stuck right now to continue my game :s

Comment
Add comment · Show 1
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 Aldwoni_legacy · Jul 18, 2011 at 12:13 PM 0
Share

function myFunction(x : int, y : int, z : int) { ... } should be: void $$anonymous$$yFunction(int x, int y, int z) {...}

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ckfinite · Jul 18, 2011 at 01:11 PM

Well, moving from top to bottom, unless you have a function that contains a yield itself, you don't need to do anything special. The function will be evaluated like it was part of the calling function's code. If you want to emulate that behavior exactly, replace yield with yield return and have the called function return either a IEnumerable or an IEnumerator.

For the second one, that function call you wrote will work in C# without changes. You aren't actually passing the function, the function is evaluated, then is given to the function you are passing it into as a parameter. If you want to pass the functions themselves, look up delegates in C#.

Lastly, I think you aren't actually ever setting the value of tile for just one or two cases, but since it works in JS, I don't really know why that wouldn't work. I need the actual error to know what the problem is.

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
avatar image
1

Answer by Bunny83 · Jul 18, 2011 at 02:52 PM

Well, macfanpro said most of this already...

In C# every function needs to specify it's return type. If a function doesn't return any value you have to use the type "void". Your first function returns a boolean (JS) / bool (C#) so you have to declare it like this:

 bool MyFunction01()
 {
     ...
     ...
     return true;
 }

Coroutines (functions that contain one or more yield-statements) returns an IEnumerator. In JS that is done implicitly but in C# you have to specify the return type.

 IEnumerator MyFunction02()
 {
     yield MyFunction01();
     Debug.Log("Finished");
 }

Also to start such a coroutine you have to use StartCoroutine(MyFunction02()) which is done behind the scenes in JS.

Like already said in your next case you don't pass a function to another function. You just pass the returned value as parameter to the function. and it works like in JS

 int getNewID()
 {
     int newID = ...;
     ...
     return newID;
 }
 
 void myFunction(int x, int y, int z)
 {
     ...
 }
 
 myFunction(getNewID(),getNewID(),getNewID());

Your "main" function problems:

Make sure that you initialize your variables. In your case if typeOfTile[0] is 3 or greater Tile would not be assigned and as long as there is one possible case the compiler will throw at least a warning. When you declare it like this: GameObject Tile = null; there will be no warning and it you get a null-ref-exception you know where it comes from.

You shouldn't use the "as"-cast unless you need it's special behaviour. A normal c-style-cast will throw a cast-exception if it can't cast the reference into the desired type. The as-cast will return null when the casting fails. Using the as cast can result in nullreference exceptions and you don't have a clue where it comes from.

Use the c-style cast:

 Tile = (GameObject)Instantiate(...);
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
avatar image
0

Answer by Ziboo · Jul 18, 2011 at 05:15 PM

Hey, Thanks a lot macfanpro & Bunny83, Taking the time to make a good and constructive answer ;)

That help me a lot, everything is working ;)

Actually, 2 of my errors came from the Resources.LoadAll.

 Object[] WallTiles = Resources.LoadAll("Dungeon/Walls");
 WallTiles[Random.Range[0,WallTiles.Length]);

This was giving me a Transform and not a GameObject, so it's why I had null exception, so I just put the typeof I wanted:

 Object[] WallTiles = Resources.LoadAll("Dungeon/Walls",typeof(GameObject))

And everything works fine.

Just to finish, I really don't get the Yield & Coroutine things, I read the doc, read your explanations, but I still don't get it:

For instance, I have my generateDungeon function, and inside some function called, but I want to wait before each are finished before going forward:

 public void generateDungeon(float _id, float _entryID, float _exitID)
 
 {
 ...
 generator.createDungeon(x, y, dungeon_objects); <- Wait before continue
 ...
 Build3DDungeon(); <- Wait before continue
 ...
 CreateMiniMap();  <- Wait before continue
 ...
 Populate();  <- Wait before continue
 ...
 SaveDungeon();
 }

I tried this:

 yield return Build3DDungeon();
 or
 yield return StartCoroutine(Build3DDungeon());

with Build3DDungeon() an Ienumerator function

But I got the error: not all code paths return a value

Does Ienumerator have to return something ? Like a bool ?

Thanks Again ! 125125125

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 Bunny83 · Jul 18, 2011 at 05:54 PM 0
Share

coroutines are used when you wnat to execute some parts later and in the meantime you yield the control back to unity and the game keeps on running. Yield just interrupts the execution at this point and it get's resumed later.

I'm not sure what your Build3DDungeon function is supposed to do. If it does something in one-run it don't need to be a coroutine. A normal function call will do. If you want to use yield inside the function you have to start it with StartCoroutine. If you want to wait for a coroutine to finish the calling function also needs to be a coroutine.

With yield return StartCoroutine(Build3DDungeon()); you wait for this coroutine to finish.

Oh and you should try to break your question into multiple questions. Asking 2 or more questions just lead to very long and confusing posts. Besides that you shouldn't use the Answer function if it's not an actual answer to the question above.

There's absolutely no problem when you answer your own question but for comments we have a comment function ;). You can also edit your question to improve it or add something if necessary.

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

6 People are following this question.

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

Related Questions

translate javascript for loop to C# 2 Answers

Weird warnings after translating javascript to C# 2 Answers

Help With Java Script - C# 1 Answer

Distribute terrain in zones 3 Answers

How to make this line work in C#? 1 Answer


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