• 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 Nalax · Nov 17, 2015 at 12:53 PM · java to c#

What is the C# equivalent of Window in JavaScript?

I'm fairly new to Unity but I've been using JavaScript in coding browser based apps for a while. Can anyone tell me what the C# equivalent of Window in JavaScript is? I am trying to access several variables using very similar code and I would like to do it using a for loop. In JavaScript I would do it like:

var north = "North"; var south = "South"; var west = "West"; var east = "East";

var directions = ["north", "south", "east", "west"];

for (i = 0; i < ; i++) { window[directions[0]] = ""; }

What would be the C# equivalent of this code, in particular the Window part? Thanks.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanoshimi · Nov 17, 2015 at 12:57 PM

The Window object only exists in the context of Javascript for the web, where it is an object reference to the parent browser window - there is no equivalent in Javascript or C# for Unity.

What are you trying to achieve? It's unclear what the function of Window in the above code is - it could simply be reduced to:

 string[] directions = {"north", "south", "east", "west"};
 for (int i=0; i<...; i++) { directions[0] = ""; }
Comment
Add comment · Show 3 · 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 Nalax · Nov 18, 2015 at 02:19 AM 0
Share

Sorry for no being more clear. I am trying to create a "snap to" mechanic for constructing sructures in my game. For each direction (north, south, east, west), I am using a function like:

      foreach (GameObject northface in north)
      {
         foreach (GameObject southface in south)
         {
             float distance = Vector3.Distance(northface.transform.position, southface.transform.position);

             if (distance < snapDistance) {
                 activeFacePosition = northface.transform.position;

                 targetFacePosition = southface.transform.position;

                 facePositionDifference = activeFacePosition - targetFacePosition;

                 transform.position += facePositionDifference;

                 normal$$anonymous$$ovement = false;
             }
         }
     }

At present I have four functions basically identical to this one, the only difference being the direction. I would like to have a loop that runs the above function four times with the different directions.

avatar image tanoshimi Nalax · Nov 18, 2015 at 10:23 AM 0
Share

So you want a single function with a switch() statement based on the direction. Although you'd be better off treating direction as an enum rather than an array of strings. Like this:

 public enum Direction { North, South, East, West }

 Direction currentDirection = Direction.South; // for example

 public void SnapTo(Direction dir){

   switch(dir){

     case Direction.North:
       // do whatever when direction is north
       break;

     case Direction.East:
       // do whatever when direction is east
       break;

     ....
   }
 }
avatar image Nalax tanoshimi · Nov 19, 2015 at 07:53 AM 0
Share

I'm not sure how the above code would simplify the need to have four copies of the function I gave in my other reply as I would still need to have each one under each of the cases of the switch statement as well as having a for loop to change the currentDirection variable to the four directions. All directions have to be run each Update. What I'm looking for is something that can be used to contstruct a variable name using the name of the direction and then the string "face" in such a way that I can then call the resulting variable name and use it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2 Programming Languages 4 Answers

Get a list of Wi-Fi networks (SSID), 0 Answers

How do perform the operations as per the commands at client side in socket unity? 0 Answers

Line not working in C#? 1 Answer

with call a plugin java in unity for usage in a application for windows 0 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