• 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
Question by jakobo55 · Jan 27, 2013 at 03:08 PM · cameravector3float

Cannot modify a value type return value of `UnityEngine.Transform.position'. Can anyone help ?

Hi. When I save t$$anonymous$$s script

 using UnityEngine;
 using System.Collections;
 
 public class Camera_Controll : MonoBehaviour {
 
 float margin = 1.5f; // space between screen border and nearest fighter
  
 private float z0 = 0; // coord z of the fighters plane
 private float zCam; // camera distance to the fighters plane
 private float wScene; // scene width
 private Transform f1; // fighter1 transform
 private Transform f2; // fighter2 transform
 private float xL; // left screen X coordinate
 private float xR; // right screen X coordinate
  
 void  calcScreen ( Transform p1 ,   Transform p2  ){
     // Calculates the xL and xR screen coordinates 
     if (p1.position.x<p2.position.x){
        xL = p1.position.x-margin;
        xR = p2.position.x+margin;
     } else {
        xL = p2.position.x-margin;
        xR = p1.position.x+margin;
     }
 }
  
 void  Start (){
     // find references to the fighters
     f1 = GameObject.Find("Player1").transform;    
     f2 = GameObject.Find("Com").transform;
     // initializes scene size and camera distance
     calcScreen(f1,f2);
     wScene = xR-xL;
     zCam = transform.position.z-z0;
 }
  
 void  Update (){
  
     calcScreen(f1,f2);
     float width = xR-xL;
     if (width>wScene){ // if fighters too far adjust camera distance
        transform.position.z = zCam*width/wScene+z0;
     }
     // centers the camera
     transform.position.x = (xR+xL)/2;
 }
 }

I have error like t$$anonymous$$s

Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable

Can anyone help ?

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Owen-Reynolds · Jan 27, 2013 at 03:22 PM

C# has a (stupid) rule that you can only change certain compound variables all at once. The $$anonymous$$nt "Consider storing the value in a temporary variable" is exactly the fix.

Would have been nice if you wrote the line number, but t$$anonymous$$s line: transform.position.z = zCam*width/wScene+z0; is it. You have to assign transform.position all at once. An easy way is:

 Vector3 TP = transform.position; TP.z= ...;  transform.position = TP;

That seems like a giant pain, but in practice, you often want to copy trans.pos into somet$$anonymous$$ng short like TP before using it a lot (and it runs a tiny bit faster.)

Comment
FL
Mehrdad995
Rioneer
jakobo55
chelder

People who like this

5 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 jakobo55 · Jan 27, 2013 at 03:38 PM 0
Share

Thanks, now I don't have any error but script don't work... I have same script in JS and it work:

 #pragma strict
 
 var margin:float = 1.5; // space between screen border and nearest fighter
  
 private var z0:float = 0; // coord z of the fighters plane
 private var zCam:float; // camera distance to the fighters plane
 private var wScene:float; // scene width
 private var f1:Transform; // fighter1 transform
 private var f2:Transform; // fighter2 transform
 private var xL:float; // left screen X coordinate
 private var xR:float; // right screen X coordinate
  
 function calcScreen(p1:Transform, p2:Transform){
     // Calculates the xL and xR screen coordinates 
     if (p1.position.x<p2.position.x){
        xL = p1.position.x-margin;
        xR = p2.position.x+margin;
     } else {
        xL = p2.position.x-margin;
        xR = p1.position.x+margin;
     }
 }
  
 function Start(){
     // find references to the fighters
     f1 = GameObject.Find("Player1").transform;    
     f2 = GameObject.Find("Com").transform;
     // initializes scene size and camera distance
     calcScreen(f1,f2);
     wScene = xR-xL;
     zCam = transform.position.z-z0;
 }
  
 function Update(){
  
     calcScreen(f1,f2);
     var width:float = xR-xL;
     if (width>wScene){ // if fighters too far adjust camera distance
        transform.position.z = zCam*width/wScene+z0;
     }
     // centers the camera
     transform.position.x = (xR+xL)/2;
 }

But I'm going to write it in C#...

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

10 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

Related Questions

Convert type UnityEngine.Vector3' to float' Error 1 Answer

Vector3(x,y,variable)? 0 Answers

Ray through camera 1 Answer

Problem with the rotation of camera in third person 1 Answer

How to detect VR camera movement? 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