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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by RasmusHallberg · Oct 25, 2016 at 09:00 AM · unity 5scripting problem

Help with js conversion to c#

So, I need some help with a js to a c# conversion and some errors, the error I get is that the transform.position doesn't work neither do GetComponent. Heres the full script:

 using UnityEngine;
 using System.Collections;
 
 public class FollowCamera : MonoBehaviour
 {
     Transform Player;
     float DistanceFromPlayer = 5;
     float StaticCameraY = 3;
 
     void Update()
     {
         transform.position.z = Player.position.z - DistanceFromPlayer; // Error in c# not js
         transform.position.x = Player.position.x + 6; // Error in c# not js
     }
 
     void LateUpdate()
     {
         GetComponent.< Camera > ().main.transform.position.y = StaticCameraY; // error in c# but not js
     }
 }

I'm pretty new to scripting so if you could describe what you're doing that would be appreciated.

The correct version in c# due to using structs(Vector3). A reference is necessary to modify the struct, this will copy the current values and allow you to modify only what is needed:

 using UnityEngine;
 using System.Collections;
 
 public class FollowCamera : MonoBehaviour
 {
     Transform Player;
     float DistanceFromPlayer = 5;
     float StaticCameraY = 3;
 
     void Update()
     {
         Vector3 tempPos = transform.position; // Get a copy of the Vector3 struct.
         
         // make modifications
         tempPos.z = Player.position.z - DistanceFromPlayer; 
         tempPos.x = Player.position.x + 6; 
         
         // Re-apply the struct
         transform.position = tempPos;
     }
 
     void LateUpdate()
     {
         Camera c = GetComponent.< Camera > ().main; // get a reference to the camera first to reuse getting the position and setting it later.
         Vector3 tempPos = c.transform.position;
         
         tempPos.y = StaticCameraY;
         
         c.transform.position = tempPos;
     }
 }
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

1 Reply

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

Answer by aditya · Oct 25, 2016 at 09:06 AM

This is because in c# you can't directly change an element of transfor.position, what you had to do is save this position in a temporary local vector3 and change the element of this vector3 and assign it back to your position ... i don't know about JS but in c# GetComponent works like this

 Getcomponent<YOUR CLASS>();
 GetComponent<YOUR CLASS>().classMember;

but in your code as you are trying to modify the position of MAIN camera then there is nothing to do with Getcomponent here, simply call

 Camera.main.transform.position

Accept if it Helps ^^

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 RasmusHallberg · Oct 25, 2016 at 09:33 AM 0
Share

Fixed the script, but thank you for makking me understand what the problem was, really helpful!

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

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

Related Questions

How To refer to other Object's Components ? 0 Answers

Scroll-view Database 0 Answers

Opening and closing a Gate on Input.GetButtonDown 0 Answers

Issues Cycling Through Weapons-C# 2 Answers

Trigger Oneshot using reticle on a VR level (C#) 0 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