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 Freakey · Nov 22, 2013 at 09:50 PM · confusedunexpectedthis

C# - Unity "this" is an unexpected symbol

Now, I am not a complete beginner with C# and know a BIT of java andjavascript, however, this is highly confusing. I am trying to create a custom character controller in C#, but it is not liking what I have put. Can anyone fix this? or even find a better way of doing this?

 void Movement() {
     if (Input.GetAxis("Horizontal") || Input.GetAxis("Vertical")) {
         moveDirection = Vector3(Input.GetAxisRaw("Horizontal"), moveDirection.y, Input.GetAxisRaw("Vertical"));
         this.transform.Translate(moveDirection * speed) * Time.deltaTime);            
     }
 }
Comment
Add comment · Show 2
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 Josh707 · Nov 22, 2013 at 09:58 PM 1
Share

'this' is unnecessary in this case, AFAI$$anonymous$$ the this keyword will refer the component or script instance on an object, in this case. For example if you call Destroy(this) it will remove this script instance from the object.

Lowercase transform is already a direct reference to this scripts attached gameobjects transform so you don't need to write this. Just transform.Translate(...) will work fine.

Also I thought setting a vector manually requires the new keyword which you seem to be missing when setting moveDirection

avatar image Freakey · Nov 23, 2013 at 10:13 AM 0
Share

Thank you for your input

1 Reply

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

Answer by aldonaletto · Nov 22, 2013 at 10:02 PM

this.transform is valid in Unity: this refers to the script, which isn't a GameObject (class where the property transform is defined), but an interesting feature in Unity allows direct access to GameObject properties from any of its components (no idea how they do it) - this.transform thus does the same as gameObject.transform or just transform. From the code posted, the problem seems to be an extra closing parenthesis after speed - just remove it:

    this.transform.Translate(moveDirection * speed * Time.deltaTime);         
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 Freakey · Nov 23, 2013 at 10:13 AM 0
Share

Thanks for the help

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

18 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

Related Questions

Difference between gameObject and GameObject and a scene item named "GameObject" and this in context of Unity grammar 2 Answers

Having issue with an object reference is required to access a non-static member. 2 Answers

Quill18's Tutorial Scripts: Unexpected Symbols 1 Answer

Error unexpected symbol 'internal' What is wrong with this? 0 Answers

error CS0664: Literal of type double cannot be implicitly converted to type `float'. Add suffix `f' to create a literal of this type 2 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