• 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 Trevus · Mar 29, 2012 at 07:38 AM · c#variabletemporarypinball

storing the value in a temporary variable problem

Hello people, i want to make a pinball game fully in C#, i am using the gameion pinball template but rewrite it in C#. i run into the following problem when firing the pinball:

if (CanUse == true) { ControlBallLaunchByKeyboard(); } else { // Spring resets back to normal position if (ballLaunch.localScale.y < 12) {

         ballLaunch.localScale.y = (ballLaunch.localScale.y + 12.0f) * 0.5f;
         ballLaunch.position.y = (ballLaunch.position.y - 63.00549f) * 0.5f;
     }        
     }
 }
 
 
 private void  ControlBallLaunchByKeyboard (){
 if (Input.GetKey(KeyCode.DownArrow))
 {
     // When the player holds down the Down Arrow button, pull down the ball launch
     if (ballLaunchForce < 2000)
     {
         float dt = Time.deltaTime * 8.0f;
         ballLaunchForce += dt * 180;
         ballLaunch.localScale.y = Mathf.Lerp(12.8f, 1.8f, ballLaunchForce / 2000);
         ballLaunch.position.y = Mathf.Lerp(-63.1f, -68.6f, ballLaunchForce / 2000);
     }
 }
 else
 {
     // Spring resets back to normal position
     if (ballLaunch.localScale.y < 12) 
     {
         ballLaunch.localScale.y = (ballLaunch.localScale.y + 12.0f) * 0.5f;
         ballLaunch.position.y = (ballLaunch.position.y - 63.00549f) * 0.5f;
     }
 }

i get allot of the following errors: - Assets/Scripts/Trig.cs(43,36): error CS1612: Cannot modify a value type return value of UnityEngine.Transform.localScale'. Consider storing the value in a temporary variable - Assets/Scripts/Trig.cs(44,36): error CS1612: Cannot modify a value type return value of UnityEngine.Transform.position'. Consider storing the value in a temporary variable

Could anyone point out what i am doin wrong or help me ??

thanks in advance

Comment
cdm89

People who like this

1 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

  • Sort: 
avatar image

Answer by moichezmoi · Mar 29, 2012 at 08:48 AM

Hi,

transform.localScale and transform.position are properties, in C#. So, you can get those vector3, you can set those vector3, but you can't dirrectly modify members of those vector3.

A solution is to use a temporary variable, and replace lines like :

 ballLaunch.localScale.y = (ballLaunch.localScale.y + 12.0f) * 0.5f;

by

 Vector3 temp = ballLaunch.localScale;
 temp.y = (ballLaunch.localScale.y + 12.0f) * 0.5f;
 ballLaunch.localScale = temp;
Comment
Bunny83
dchen05
TrickyHandz
efge
Damonic217
atifmahmood29
ltgbau
shavemadox
Rexor4321
Ryebread5

People who like this

10 Show 6 · 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 Trevus · Mar 29, 2012 at 10:55 AM 0
Share

Thanks MoichezMoi,

I inserted your solution, and it worked. The errors are gone now.

Some new ones arrived when i start the game. Do you know what it means?

what i am ingame trying to do is when arrowkey down is pressed it should build up force and show the spring pulling back and then when release the spring resets and gives the ball force.

NullReferenceException UnityEngine.Transform.get_localScale () (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:78) Trig.Update () (at Assets/Scripts/Trig.cs:40)

when i click on the error, it says the error lies somewhere here: if (ballLaunch.localScale.y < 12)

avatar image Bunny83 · Mar 29, 2012 at 10:58 AM 2
Share

I would guess your "ballLaunch" variable is not set to any object. Where do you assign a Transform to it?

avatar image erickalvessud · Oct 08, 2013 at 02:45 AM 0
Share

Thank you man. You helped me too.

avatar image marcfielding · Jan 10, 2014 at 01:16 AM 0
Share

Helped me too thanks a lot!

avatar image RyanPate52 · Feb 01, 2015 at 05:45 AM 0
Share

Thanks for the help!

Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

converting javascript to c# 1 Answer

Converting this JS code to C# 1 Answer

variable js to c# 1 Answer

Use of unassigned local variable `selection' Destroying instance 1 Answer

Multiple Cars not working 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