• 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 jadaith · Oct 04, 2013 at 06:12 AM · c#javascriptscritping

Running into a Syntax error converting from JS to C#

I've looked and saw various answers on this topic and none seem to work for me. I'm using the incorrect syntax when writing this line of code in c#

 newStructure.transform.localEulerAngles.y = (Random.Range(0,360));

Whenever I try to do a new Vector3 it gives me errors as well. What is the proper syntax for a problem like this?

Comment
Add comment · Show 5
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 AndyMartin458 · Oct 04, 2013 at 06:17 AM 2
Share

It kind of stinks, but you cannot set part of a vector. You have to create a new transform all together. When you were creating new transform, did you only try to set the y value? Have you tried something like this.

 newStructure.transform.localEulerAngles = new Vector3(  
        newStructure.transform.localEulerAngles.x,
        Random.Range(0,360),
        newStructure.transform.localEulerAngles.z);
avatar image vexe AndyMartin458 · Oct 04, 2013 at 06:21 AM 0
Share

Actually, you can set a part of a VectorX just fine:

 Vector3 v = new Vector3();
 v.x = 1;
 v.y = 2;
 v.z = 3;

But when you're modifying a Vector3 from a transform:

transform.position.x = 1;

you'll get a "Cannot modify the expression because it is not a variable"

avatar image jadaith AndyMartin458 · Oct 04, 2013 at 06:22 AM 0
Share

Both of you guys method worked and I see what I did wrong on both.

For your method when I tried to add the new vector 3 I still had localEulerAngles.y everything else I wrote the same as yours thanks for that visual display.

avatar image AndyMartin458 AndyMartin458 · Oct 04, 2013 at 06:26 AM 0
Share

I just wanted to make sure that all that was modified was the y. @vexe I see what you mean, yes, you can directly modify a Vector3 but not as part of a transform.

avatar image vexe · Oct 04, 2013 at 06:32 AM 0
Share

Please don't say Java it's JavaScript, both are separate worlds :) - I edited your title.

1 Reply

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

Answer by vexe · Oct 04, 2013 at 06:16 AM

You don't modify the individual coordinates of a transform's Vector3 (let it be a position, euler angles, etc), instead create a new one, modify it and then assign:

 Vector3 v = new Vector3(someValue, Random.Range(0,360), someValue);
 newStructure.transform.localEulerAngles = v;

or of course do it right away (AndyMartin's answer)

If you thought that was long, you could this instead (same effect):

 newStructure.transform.localEulerAngles -= new Vector3(
          0,
          newStructure.transform.localEulerAngles.y + Random.Range(0,360),
          0);
Comment
Add comment · Show 6 · 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 jadaith · Oct 04, 2013 at 06:23 AM 0
Share

I tried the same method at which you used before and I noticed now where I messed up I still had localEulerAngles.y when I tried to use the new value. Thanks for your help.

avatar image jadaith · Oct 04, 2013 at 06:33 AM 0
Share

Thanks for that advice I've never seen it done that way before.

avatar image AndyMartin458 · Oct 04, 2013 at 06:42 AM 2
Share

hey my answer is kind of copied into this one with no votes for me :(

avatar image jadaith · Oct 04, 2013 at 06:48 AM 1
Share

haha tbh I was trying to give you both the correct answer thingy but it was too late. Your awesome though man XD

avatar image vexe · Oct 04, 2013 at 06:56 AM 0
Share
  • Yep it's upsetting. I admit that copied, just for the sake of a complete answer. But now I've added a ref ins$$anonymous$$d :D

Show more comments

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Javascript to C# Conversion Question 1 Answer

Does Unity Script syntax change from release to release? If so how much? 4 Answers

Changing Value in JavaScript from 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