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
-1
Question by Griffo · Mar 20, 2014 at 03:11 PM · js-c#js - c#

.js to C# conversion

Hi,

I'm using the below function to get the number from the game objects name or tag in .js

What I need to do is add the same function to a C# script but I really don't know how to write this in C#

Can someone help me out please .. Cheers.

 // -----------------------------------------------------------------
 // Function to extract the number from the GameObjectX.tag or .name
 // -----------------------------------------------------------------
 function ConvertToInt(stringContainingNumber : String) : int{
    return System.Convert.ToInt32(stringContainingNumber.Substring(stringContainingNumber.ToList().FindIndex(function(c) char.IsDigit(c))));
 }
 // -----------------------------------------------------------------
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

4 Replies

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

Answer by Gruffy · Mar 20, 2014 at 03:54 PM

Hey Griffo, I have edited my answer AGAIN. this should give you the result you wanted. take care for now dude.

This is now arbitrary and requires no special set up. just add this to a gameobject or camera, then add the gameobject you want to it. form there , you can call your gameobject whatever you like, the regex will search until the first number appears.

I have coded it to show up in the debug.log, of which you can take that value held in result and do what you like with it as an integer.

returning it(i.e. renaming the go.gameObject.nameis another thing but probably just a reverse of this in some way) take care and thought this was a great question. Gruffy

Newly Edited code below

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Text.RegularExpressions;
 
 
 public class UsingRegExp : MonoBehaviour 
 {
     public GameObject go;
     private string cachedStringForParsing = null;
     private string resultingValueFound = null;
     private string regexPattern = @"\d";
     private int result = 0;
     private Regex regex;
     private Match match;
     // Use this for initialization
     void Start () 
     {
         cachedStringForParsing = go.gameObject.name;
         Debug.Log(cachedStringForParsing);
 
     }
     private void ConvertStringtoInt(string value)
     {
         try
         {
            result = Int32.Parse(value);
           // Debug.Log(result.ToString());
         }
         catch (FormatException e)
         {
             Debug.LogException(e);
             return;
         }
     }
     // Update is called once per frame
     void Update () 
     {
         //resultingValueFound = cachedStringForParsing;
 
         resultingValueFound = Regex.Match(cachedStringForParsing, regexPattern).Value;
 
         ConvertStringtoInt(resultingValueFound);        
         Debug.Log("This " + result + " is now cached in integer an form");
     }
 }
 
Comment
Add comment · Show 13 · 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 Griffo · Mar 20, 2014 at 04:07 PM 0
Share

Hi,

I get an error .. The name `ConvertToInt' does not exist in the current context

with the above code.

avatar image adsamcik · Mar 20, 2014 at 04:16 PM 0
Share

you must write on the top using System; or replace ConvertToInt by System.Convert.ToInt32

avatar image Griffo · Mar 20, 2014 at 04:45 PM 0
Share

O$$anonymous$$, I'd already got using System.Linq; at the top so I replaced ConvertToInt to System.Convert.ToInt32, and that got rid of the error, but when I call the function with ConvertStringToInt(transform.tag); I get this error ..

FormatException: Input string was not in the correct format

On the line .. int converted = System.Convert.ToInt32(str);

avatar image adsamcik · Mar 20, 2014 at 04:50 PM 0
Share

I don't think that for example "Untagged" can be converted to numbers.

avatar image Gruffy · Mar 20, 2014 at 07:42 PM 1
Share

So I have to eat, so Im told.. So here is my code so far.

i think it`ll give a format exception error, so will need some more looking at . I will be bakc in a mo, but if you fancied looking about / or over this code, you should get the gist as we are only utilizing the RegEx class of .NET.

 public class UsingRegExp : $$anonymous$$onoBehaviour 
 {
     public GameObject go;
     private String cachedStringForParsing = null;
     private String resultingValueFound = null;
     private int result = 0;
     // Use this for initialization
     void Start () 
     {
         cachedStringForParsing = go.gameObject.name;
         Debug.Log(cachedStringForParsing);
 
     }
 
 
     // Update is called once per frame
     void Update () 
     {
         resultingValueFound = cachedStringForParsing;
 
         resultingValueFound = Regex.$$anonymous$$atch(cachedStringForParsing, @"/d+").Value;
         Debug.Log(resultingValueFound);
 
         Int32.Parse(resultingValueFound);
 
         Debug.Log("This" + resultingValueFound + " is now cached in integer form");
 
 
     }
 }
Show more comments
avatar image
0

Answer by adsamcik · Mar 20, 2014 at 03:20 PM

This might help http://www.m2h.nl/files/js_to_c.php

You just have to rewrite data type if there is var in js, because js doesn't specify data types.

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 Griffo · Mar 20, 2014 at 04:06 PM 0
Share

O$$anonymous$$, using the converter I get ..

 int ConvertToInt ( string stringContainingNumber ){ return System.Convert.ToInt32(stringContainingNumber.Substring(stringContainingNumber.ToList().FindIndex(function(c) char.IsDigit©))); }

And get the error .. error CS1525: Unexpected symbol `char'

avatar image
0

Answer by Griffo · Mar 20, 2014 at 07:51 PM

Think I've found the answer ..

 string numbersOnly = Regex.Replace(transform.tag, "[^0-9]", "");

Adding ..

 using System.Text.RegularExpressions;

At the top, seem to work .. Then turn numbersOnly into a int.

 int tagNumber = Convert.ToInt32(numbersOnly);
Comment
Add comment · Show 2 · 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 Gruffy · Mar 20, 2014 at 08:19 PM 0
Share

yup, sounds similar to where the code i recently posted was going You may be better off with Parse() ins$$anonymous$$d of Convert.ToInt(). Im just looking at it now

avatar image Gruffy · Mar 20, 2014 at 08:51 PM 0
Share

Hey Griffo, I did it, and wrapped it up nice for you in my edited answer. the whole issue i had all along (format exception error) was due to my stupidity where is should have types @"\d+" i did in fact type @"/d+" causing a mahoosive issue for obvious reasons based around parsing structure of regex itself(forward and back slashes having signifcantly different meanings etc) anyway, like I said, solved for you in my edited Answer above. Take care Gruffy

avatar image
0

Answer by Penzin · Apr 05, 2014 at 07:08 AM

This function takes a string and constructs a list containing all ints found in the string.

 public List<int> ConvertToInt(string Input)
 {
     List<int> Results = new List<int>(Input.Length);
 
     for (int i = 0; i < Input.Length; i++)
     {
         if (char.IsDigit(Input[i]))
         {
             Results.Add(Convert.ToInt32(char.GetNumericValue(Input[i])));
         }
     }
 
     Results.TrimExcess();
 
     return Results;
 }
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

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Create a guitexture When clicked on 3d text? 0 Answers

js to C# converstion Problem 3 Answers

Convert js to C# Serializer problem 1 Answer

Converting from js to 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