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

Converting to C#

Discussion in 'Scripting' started by darugylsweater, May 14, 2015.

  1. darugylsweater

    darugylsweater

    Joined:
    Oct 26, 2013
    Posts:
    17
    I'm converting a script to C# to make it easier for the rest of my coding team but this line of code
    Code (CSharp):
    1. if (Input.GetKey(KeyCode.Space))
    2.             {
    3.                 gameObject.GetComponent<Rigidbody>().velocity = gameObject.GetComponent<Rigidbody>().velocity * 0.9f;
    4.             }
    is giving me the error "cannot modify a value type return value of unityengine.Rigidbody.velocity'.Consider storing the value in a temporary variable" and i'm sure its some stupid little thing but I don't know the answer
     
  2. Akinon

    Akinon

    Joined:
    Oct 29, 2012
    Posts:
    9
    I may be wrong, but if I remember right, the velocity variable in a rigidbody is a vector3 isn't it? if you're using the spacebar, you are probably jumping yes? try this

    Code (csharp):
    1.  
    2. gameObject.GetComponent<Rigidbody>().velocity.y *= 0.9f;
    3.  
    also you shouldn't have to access it a second time. using the *= does the same thing and saves a bit of processing if I remember correctly

    Edit: also the AddForce() function may be more what you're looking for, bit smoother transitioning
     
  3. darugylsweater

    darugylsweater

    Joined:
    Oct 26, 2013
    Posts:
    17
    That code didn't do anything, i'm still getting the same error, also i would use addforce but i want an abrupt stop not a smooth stop
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Its one of the synaptic sugar items that is hidden in JS. Try this.

    Code (CSharp):
    1. if (Input.GetKey(KeyCode.Space))
    2.             {
    3.                 Vector3 temp =  GetComponent<Rigidbody>().velocity * 0.9f;
    4.                 GetComponent<Rigidbody>().velocity = temp;
    5.             }
    Its also worth caching the rigid body if you call GetComponent frequently.
     
  5. darugylsweater

    darugylsweater

    Joined:
    Oct 26, 2013
    Posts:
    17
    Thank you
     
    Kiwasi likes this.
  6. darugylsweater

    darugylsweater

    Joined:
    Oct 26, 2013
    Posts:
    17
    one final thing its giving the same error for this line of code as well
    Code (CSharp):
    1.         } else {
    2.             gameObject.GetComponent<Rigidbody>().velocity.x = -topSpeed;
    3.        
    topSpeed is a float
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Same solution.

    Code (CSharp):
    1. Vector3 temp = GetComponent<Rigidbody>().velocity;
    2. temp.x = -topSpeed;
    3. GetComponent<Rigidbody>().velocity = temp;
     
  8. darugylsweater

    darugylsweater

    Joined:
    Oct 26, 2013
    Posts:
    17
    thanks i could not figure out which part was the temp