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 goibon · Jun 15, 2020 at 11:16 AM · testingserializedobject

Why are the fields of my SerializedObject null when I set another field to its default value?

I am trying to learn more about how to write play mode tests in Unity3D and I am facing a peculiar behaviour in one of my tests. I am using SerializedObject to set the values of protected fields of my ScriptableObject class CurvedMovement but whenever I attempt to set the _speed field to the same value as its default (50) then the other fields, _xCurve and _yCurve, are null. I've tried testing with loads of different values and even tried changing the default value in the abstract class Movement but every time the test sets the _speed field to exactly the same value as its default value then the curves are null. Can anyone help me understand why this happens and how can I write tests without them breaking if the default value of the Movement class changes?

The class I am testing implements the following abstract class Movement:

 public abstract class Movement : ScriptableObject, IMovement
 {
     [SerializeField]
     [Tooltip("The speed at which the object moves.")]
     protected float _speed = 50f;

     public abstract void Move(Rigidbody2D rigidbody);
 }


The implementation I am currently testing is this:

 [CreateAssetMenu(menuName = "Movement/Curved Movement")]
 public class CurvedMovement : Movement
 {
     [SerializeField]
     [Tooltip("The curve that the unit will follow along the x-axis when moving.")]
     protected AnimationCurve _xCurve;

     [SerializeField]
     [Tooltip("The curve that the unit will follow along the y-axis when moving.")]
     protected AnimationCurve _yCurve;
 
     public override void Move(Rigidbody2D rigidbody)
     {
         var movementVector = new Vector2(_xCurve.Evaluate(Time.time) * _speed, _yCurve.Evaluate(Time.time) * _speed);
         rigidbody.MovePosition(rigidbody.position + movementVector * Time.fixedDeltaTime);
     }
 }

My test is here:

 [UnityTest]
         public IEnumerator CurvedMovementWithSpeedGreaterThanZeroAndNoCurveDoesNotMoveTheRigidBody()
         {
             var speed = 50f;
             var curvedMovement = ScriptableObject.CreateInstance<CurvedMovement>();
             var serializedObject = new SerializedObject(curvedMovement);
             serializedObject.FindProperty("_speed").floatValue = speed;
             serializedObject.FindProperty("_xCurve").animationCurveValue = new AnimationCurve();
             serializedObject.FindProperty("_yCurve").animationCurveValue = new AnimationCurve();
             serializedObject.ApplyModifiedProperties();
 
             var gameObject = new GameObject();
             var rigidbody2D = gameObject.AddComponent<Rigidbody2D>();
             rigidbody2D.gravityScale = 0;
             var initialPosition = rigidbody2D.position;
             curvedMovement.Move(rigidbody2D);
 
             yield return new WaitForFixedUpdate();
 
             Assert.AreEqual(initialPosition.x, rigidbody2D.position.x);
             Assert.AreEqual(initialPosition.y, rigidbody2D.position.y);
 
             GameObject.Destroy(gameObject);
         }
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

0 Replies

· Add your reply
  • Sort: 

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

127 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 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 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 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 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

Checklist in preparing a game for public release. 2 Answers

I can only build games for andriod but I have an iOS product, is there any way I can like download a converter or compiler so I can test my games on my iOS product 1 Answer

Updating an existing build on Android device 1 Answer

What is a good beta-delivery solution? 1 Answer

Is it possible to prevent AssertionComponent from being stripped out of non-dev builds? 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