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 /
  • Help Room /
avatar image
0
Question by Kurns · Dec 30, 2015 at 01:49 PM · c#javascriptfunction

How do I convert this into C# and where should this be added?

I know JavaScript, but I´m new into C# , I could use some help. The function is JavaScript and the other code is in C#. The desired result is to make the text object to be the value of the "findPrimeNums" function output. How should I change the function, how do I call it and where should it be in the code? I want to merge these codes into one, C#. Thanks in advance.

The prime function is here:

function findPrimeNums(n) {

    var x= 3,j = 1,i=2,

    primeArr=[2],isPrime;
    for (;x<=n;x+=2){
        j = Math.sqrt (x);
        isPrime = true;
        for (i = 2; i <= j; i++)
        {
             if (x % i == 0){
                 isPrime = false;
                 break;
             }
         }
         if(isPrime){
             primeArr.push(x);
         }

     }   

     for (var e = 1; e < primeArr.length; e++) {
     console.log(primeArr[e])
     }
 }
 


The rest (C# code) is here:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TextChanger : MonoBehaviour {
 
 //function here?
 
  void Start () {
         GameObject myTextGo = GameObject.Find("MyText");
         Text textObject = myTextGo.GetComponent<Text>();
         textObject.text = //function result
     }
     
 }









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 gjf · Dec 30, 2015 at 05:46 PM 1
Share

you might try some c# tutorials to get a little more experience about how things work - what you're asking (where you put it and how you call it) are some of the first things you'd learn...

regarding the code, did you look for code converters or try to convert it yourself? the syntax is remarkably similar...

hopefully you've done all of this in the 3 hours since you posted the question ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jmonasterio · Dec 30, 2015 at 06:32 PM

For a common algorithm like this, the best thing to do would be to google for a C# function that does what you want:

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%23+code+to+list+prime+numbers

But, I just did a straighforward port. It's not very good code to start with, so you may want to throw this away and start over anyway. Maybe this will help you learn, though:

 void findPrimeNums(int n)
     {
         var primeArr = new List<int>();
         bool isPrime;
         for (int x=3; x <= n; x += 2)
         {
             int j = (int) Mathf.Sqrt(x);
             isPrime = true;
             for (int i = 2; i <= j; i++)
             {
                 if (x % i == 0)
                 {
                     isPrime = false;
                     break;
                 }
             }
             if (isPrime)
             {
                 primeArr.Add(x);
             }
         }
         for (int e = 0; e < primeArr.Count; e++) // Starting at zero here. Original code probably skipped first.
         {
             print(primeArr[e]);
         }
     }
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 jmonasterio · Dec 30, 2015 at 06:55 PM 0
Share

Add the above function where you had the comment:

 //function here?
avatar image jmonasterio · Dec 30, 2015 at 06:57 PM 0
Share

Also note that the function you supplied doesn't return any values, so you can't really use it like you want:

 textObject.text = findPrimeNums(100); // Won't work.

Not really clear what you want to do, since the function you started with printed out the results to console ins$$anonymous$$d of returning anything.

avatar image Kurns · Dec 31, 2015 at 08:02 AM 0
Share

Great answer, thanks for your help! I was just wondering, why doesn't the textObject.text = findPrimeNums(100); work. Will adding .toString do the job or what should I do?

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

60 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 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 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

GameJolt API simple Score 0 Answers

Running C# Function from JS file 0 Answers

Any way to use UI on a screen renderer? 0 Answers

addComponent c# script by java 0 Answers

Jump Issue :( 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