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 alexanderameye · Dec 26, 2014 at 06:35 PM · positionvaluedoormodify

Unity error, can't modify transform.position value

So I have a door in my scene and I want a script (attached to the door) that adds an empty gameobject (hinge) to the scene, sets the position of the hinge to be in the left-down corner of the the door, and make the hinge a parent to the door so I can later on transform that empty gameobject with eulerangles, and the door will move with it.

I have this script:

 using UnityEngine;
 using System.Collections;
 
 public class DoorScript : MonoBehaviour {
 
     GameObject hinge;
     public Transform door;
 
     // Use this for initialization
     void Start () {
         hinge = new GameObject();
         hinge.name = "hinge";
 
         float PosDoorX = door.transform.position.x;
         float PosDoorY = door.transform.position.y;
         float PosDoorZ = door.transform.position.z;
 
         float ScaleDoorX = door.transform.localScale.x;
         float ScaleDoorY = door.transform.localScale.y;
     
         hinge.transform.position.x = PosDoorX + ScaleDoorX / 2;
         hinge.transform.position.y = PosDoorY - ScaleDoorY / 2;
         hinge.transform.position.z = PosDoorZ;
 
         door.transform.parent = hinge.transform;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }


It returns this error: 'Assets/Scripts/DoorScript.cs(21,33): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable '

I know it means that I cant do math with the transform values, but how can I fix this?

tyvm!

Alexander

Comment
Add comment · Show 1
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 alexanderameye · Dec 26, 2014 at 06:44 PM 0
Share

I basically want the game object to be in the left down corner of EVERY door, whatever the size or scale is. so I can just add the script to every door, and when I press play, there is an empty gameobject in the left corner of EVERY door.

1 Reply

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

Answer by Landern · Dec 26, 2014 at 07:00 PM

in C# you can't modify a struct directly, in unity/javascript it "appears" you can but during the script building process it does the same as i'm displaying below:

 using UnityEngine;
 using System.Collections;
 
 public class DoorScript : MonoBehaviour {
 
     GameObject hinge;
     public Transform door;
 
     // Use this for initialization
     void Start () {
         hinge = new GameObject();
         hinge.name = "hinge";
 
         float PosDoorX = door.transform.position.x;
         float PosDoorY = door.transform.position.y;
         float PosDoorZ = door.transform.position.z;
 
         float ScaleDoorX = door.transform.localScale.x;
         float ScaleDoorY = door.transform.localScale.y;
 
         Vector3 posCopy = hinge.transform.position; // make a copy of the Vector3
 
         posCopy.x = PosDoorX + ScaleDoorX / 2;
         posCopy.y = PosDoorY - ScaleDoorY / 2;
         posCopy.z = PosDoorZ;

         hinge.transform.position = posCopy; // Reassign the Vector3 copy back to position

         door.transform.parent = hinge.transform;
     }
 
     // Update is called once per frame
     void Update () {
 
     }
 }
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 alexanderameye · Dec 26, 2014 at 08:07 PM 0
Share

Works like a charm, tyvm Landern!

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

27 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

C# Check If Scripted Gameobject goes Past Variable Gameobject 0 Answers

Modify circular progress bar based on a value? 1 Answer

portal problem 1 Answer

Desert Fortress Pack asset store 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