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 b2przemo · Nov 13, 2014 at 07:54 PM ·

Converting scripts between javascript and c#

Hi, I try to rewrite js controller script in C Sharp. In my js file I've many classes whose only storing some variables. Similar construction won't work in C#, because those variables are unreachable. For example

js

 class Aaa{
  var abc = 1.5;
 }
 
 var d:Aaa;

in c# i try this

 class Aaa{
    public float abc = 1.5f;
 }
 public Aaa d = new Aaa();
 d.abc = <new value>

This construct give me NullReferenceException. Where I've made mistake?

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

Answer by zharik86 · Nov 13, 2014 at 07:57 PM

Try this:

  public class Aaa { //add "public" modificator, without variable/class is "private"
   public float abc = 1.5f;
  }
  public Aaa d = new Aaa();

  void Start() {
   d.abc = 2.0f;
  }

Initialization your variable of class in Awake() or Start(). I hope that it will help you.

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 b2przemo · Nov 13, 2014 at 08:25 PM 0
Share

Thanks for respond :) I set this classes with all vars to public but seems that this construct won't work also. This is quite strange for me because this code is quite simple

 public class SomeClass:$$anonymous$$onoBehavior {
    public class Aaa() {
      public float abc =1.5f;
    }
 
    public Aaa d; // with = new Aaa() unity throw me StackOverflowException :)
 
   void Start() {  //or Awake
     d.abc <---- at this level even Debug.Log give me NullReferenceException
    }
 }

Im stuck with this, and don't know how reach this variables. Dosn't matter is this float vector or bool I can't set or get this value 8-(

avatar image zharik86 b2przemo · Nov 25, 2014 at 07:50 AM 0
Share

@b2prezmo Correct create your class:

  public class Aaa() { //wrong, delete "()"

And see my example. For me all works.

avatar image
0

Answer by Jeff-Kesselman · Nov 13, 2014 at 08:27 PM

Your problem is that your code is recursively constructing...

Your class Aaa has a field d which is initialized by constructing a new Aaa,

the new Aaa has a field d which is initialized by constructing another Aaa,

that has a field d which is initialized by constructing another Aaa....

And it goes on forever, but since computer are finite, it eventually runs out of stack room and crashes.

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 b2przemo · Nov 13, 2014 at 08:37 PM 0
Share

I've noticed that, but I wonder how can it be: in js all code works fine, but similar construct in C won't work. Unity don't throw stackoverflow error if declaration is simple : public Aaa d; ins$$anonymous$$d public Aaa d = new Aaa(). Besides this, script stop at Awake or Start function where I try to get some variables from this classes.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Need help converting js to C# 1 Answer

How to fix this for c#? 1 Answer

C# score not updating 1 Answer

Type `GUIText' does not contain a definition for `text' 2 Answers

If you hold down button *BUTTON*, speed boost. 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