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 coolbird22 · Sep 01, 2014 at 05:09 AM · transformlistsavevalues

How to assign saved transform values(tr,rot,sc) from a List, to the same gameobject ?

I've managed to save transform values (translates,rotations,scales) of my gameobject (on each frame) into a List. Now, I'm unable to assign these same values to my gameobjects transform. When I do so, I get this error -

cs(34,28): error CS0200: Property or indexer `UnityEngine.GameObject.transform' cannot be assigned to (it is read only)

Here is the saving to the list script I'm using -

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class saveList : MonoBehaviour {
     private List<Transform> Movements = new List<Transform>();
     private int MovementIndex = 0;
     private bool reassigning;
     
     void  Update (){
         if(!reassigning)
         {
             Movements.Add(transform);
             MovementIndex++;
         }
         
         if(MovementIndex > Movements.Count - 1)
         {
             MovementIndex = Movements.Count;
         }
         
         if(Input.GetKey("e"))
         {
             reassigning = true;
             Rewind();
         }else
         {
             reassigning = false;
         }
     }
     
     void  Reassign (){
         MovementIndex--;
         transform = Movements[MovementIndex];
         //Movements.RemoveAt(MovementIndex);
     }
 }
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
3
Best Answer

Answer by zharik86 · Sep 01, 2014 at 06:28 AM

Unfortunately, you so simply can't save your Transform for different statuses. You remember the reference to object. It always same. Therefore your status not to change. And the second, it is necessary to change each Transform element manually (position, rptatio, scale). Typically, you'll either want to do a 'shallow copy' or a 'deep copy' when copying fields from one instance of a class to another instance of the same class type. I will write a simple code with comments:

  //Create class for our Transform with correct variable
  public class myTrans {
   public Vector3 myPos = Vector3.zero; //current position
   public Quaternion myQuat = Quaternion.identity; //current rotation
   public Vector3 myScale = new Vector3(1, 1, 1); //current scale
  }

  private List<myTrans> Movements = new List<myTrans>(); //create list of our class
  private int MovementIndex = 0;
  private bool reassigning;
 
  void  Update (){
   if(!reassigning) {
    myTrans tp = new myTrans(); //create temp variable our class
    tp.myPos = this.transform.position; //save current position
    tp.myQuat = this.transform.rotation; //save current rotation
    tp.myScale = this.transform.localScale; //save current scale
    Movements.Add(tp); //add current data in our list
    MovementIndex++;
   }
   if(MovementIndex > Movements.Count - 1) {
    MovementIndex = Movements.Count;
   }
   if(Input.GetKey("e")) {
    reassigning = true;
    Reassign(); //maybe change function Rewind() to Reassign()
   } else
    reassigning = false;
   }
  }
 
  void  Reassign (){
   MovementIndex--;
   //check index and count list
   if (MovementIndex >= 0 && Movements.Count > 0) {
    this.transform.position = Movements[MovementIndex].myPos;
    this.transform.rotation = Movements[MovementIndex].myQuat;
    this.transform.localScale = Movements[MovementIndex].myScale;
   }
   //Movements.RemoveAt(MovementIndex);
  }

I hope that it will help you.

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 coolbird22 · Sep 01, 2014 at 06:34 PM 0
Share

Yes. This worked like I had expected it to work. Thanks a bunch for sharing !

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

22 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

Related Questions

A node in a childnode? 1 Answer

use list of gameobjects transform in raycast 0 Answers

Calling GameObject's transform from list 1 Answer

C#, expensive or inexpensive? 1 Answer

how to get the snake body parts to follow the snake head? 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