Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Consider Storing in a Temporary Variable?

Discussion in 'Scripting' started by LegendXV, Jun 3, 2015.

  1. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    The error is
    Assets/Resources/Scripts/Clickable.cs(35,30): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable

    It's located in my LateUpdate function, where GameObject go is located at. However ... I set go equal to an instantiated GameObject, so I don't know why I can't access it again.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class Clickable : MonoBehaviour {
    7.  
    8.     private Player mainPlayer;
    9.     private int number;
    10.     private bool clickedOn = false;
    11.     public float targetScale = .9f;
    12.     public float shrinkSpeed = .1f;
    13.     public bool shrinking = true;
    14.     public GameObject hover;
    15.     private GameObject go;
    16.  
    17.     // Use this for initialization
    18.    
    19.     void Start() {
    20.  
    21.     }
    22.  
    23.     public void initiate(Player p, int numb) {
    24.  
    25.         // Passes player and number to generate.
    26.         mainPlayer = p;
    27.         number = numb;
    28.  
    29.         // Hovering Text
    30.         go = Instantiate (hover, new Vector3 (0, 1, 0), Quaternion.identity) as GameObject;
    31.  
    32.     }
    33.    
    34.     // Update is called once per frame
    35.     void Update () {
    36.         /*
    37.         if (shrinking) {
    38.             if (targetScale >= this.transform.localScale.x) {
    39.                 print ("Done!");
    40.                 shrinking = false;
    41.             }
    42.             this.transform.localScale = Vector3.Lerp (this.transform.localScale, new Vector3 (targetScale, targetScale, targetScale), Time.deltaTime * shrinkSpeed);
    43.             print ("X " + this.transform.localScale.x);
    44.         }*/
    45.     }
    46.  
    47.     void LateUpdate() {
    48.         go.transform.position.x = this.transform.position.x;
    49.         go.transform.position.z = this.transform.position.z;
    50.     }
     
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    Transform.position is a property - it returns to you a Vector3 and you can set it as a Vector3, but you can't modify the individual components. Get it out into a Vector3 first:

    Code (CSharp):
    1. Vector3 pos = go.transform.position;
    then modify the new Vector3:

    Code (CSharp):
    1. pos.x = this.transform.position.x;
    And finally, set it back:

    Code (CSharp):
    1. go.transform.position = pos;
     
    dansav and Kiwasi like this.
  3. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    To add to that, something else you can do is create extension methods to make this easier:

    Code (CSharp):
    1. public static void SetXPosition(this Transform t, float val)
    2. {
    3.     Vector3 temp = t.position;
    4.     temp.x = val;
    5.     t.position = temp;
    6. }
    If you had that, then you could just say:

    Code (CSharp):
    1. go.transform.SetXPosition(this.transform.position.x);
    And it would do that process for you each time.
     
  4. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    Thanks a lot, it worked. Bummed me out so much. Also, can you take a look and see why my gameobject isn't appearing? I've set it to spawn a bunch of 3D texts over each object but it's not spawning.

    Edit: Nvm got it.
    Edit: Why is my Update() { function not "updating"? I have it so it prints "Hello" but it only prints it once and then stops. It's also not updating the pos of my GameObject. It just stays set at its original spawn.
    Edit: Nvm, got it, silly me. Thanks :)
     
    Last edited: Jun 3, 2015
  5. dansav

    dansav

    Joined:
    Sep 22, 2005
    Posts:
    499


    JasonBricco: Thanks, your explanation is very clear. Unity should include it in the Debug instead of: Consider storing the value in a temporary variable.

    I was making the components x,y,z into temporary variables which wasn't working at all. I understood it immediately after reading your explanation.
     
    Last edited: Oct 12, 2018
  6. Rispat-Momit

    Rispat-Momit

    Joined:
    Feb 14, 2013
    Posts:
    261
    It is driving me crazy! The temporary variables are not at all accurate!

    This doesn't even work properly! It glitches in between sizes!:

    Code (CSharp):
    1.  Vector2 Fx = MyRect.sizeDelta;
    2.         Vector2 Wx = MyWall.sizeDelta;
    3.  
    4.  
    5.  
    6.         if (Fx.x > Wx .x)
    7.         {
    8.  
    9.           Fx.x = Wx .x;
    10.  
    11.         }
    On the other hand, this works perfectly but I can't access the X axis:

    Code (CSharp):
    1.  
    2.  
    3.           MyRect.sizeDelta = MyWall.sizeDelta;
    4.  
    5.        
    Please help me :(:(:(:(:(
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Take a deep breath and please don't post new questions to a 2+year old post.

    Instead, make your own fresh post... it's FREE!

    When you do, we are NOT mind-readers, so keep these steps in mind:

    How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220

    Also, if you want to learn more about what is going on, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
    Rispat-Momit likes this.