Script Component Not Being Added To GameObject On Project Initialization

Note: My apologies, I posted this same question on the forums… unsure if I should be using Unity Forums or Unity Discussions for stuff like this.

Hello Unity forum!

I’ve been banging my head against this bug for the better part of three days now and I thought that I should reach out to the experts rather than continue to spin my wheels trying to figure this one out alone. Bear with me, this is a pretty strange one.

In short, I have a specific component attached to my Player GameObject that is not loading upon first booting up my Unity (v2023.1.4f1) project. However, after I remove and add this component back onto the Player GameObject, it works.

My Player movement is controlled by Rigidbody rather than CharacterController. This means that, to prevent jittering caused by the physics and framerate not being in sync, children of Player need to have their positions interpolated. I am interpolating these positions in three distinct ways:

  1. Setting the “Interpolate” setting on Player’s Rigidbody component to the “Interpolate” setting.
  2. Attaching a custom script “Smooth Camera” to the Player’s camera component.
  3. Attaching a custom script “Smooth Container” to the Player’s body component.

This current setup is working almost perfectly, with the one exception: the “Smooth Container” script needs to be removed and readded to it’s component in order for everything to run smoothly. This is a issue seeing as to how this would be impossible in a standalone build of the game.

Here is my current Player hierarchy:

  • Player (Rigidbody on this object has “Interpolate” setting enabled)
    • body (“Smooth Container” script exists on this GameObject)
      • arms mesh
      • armature
        • camera (“Smooth Camera” script exists on this GameObject)
        • etc.

Here is the “Smooth Container” script:

//  class   : SmoothContainer.cs
//  purpose : lerp transform contents to follow the player's position
//  created : 2023-07-11
using UnityEngine;
public class SmoothContainer : MonoBehaviour {
  [SerializeField] private float moveSpeed = 100f;
  [SerializeField] private float distanceLimit = 1f;
  [SerializeField] private PlayerController controller;
  private Vector3 oldPos;
  private void Awake() {
    Debug.Log("Log: SmoothContainer Awake() hit...");
    oldPos = transform.position;
  private void LateUpdate() {
    Debug.Log("Log: SmoothContainer LateUpdate() hit...");
    Vector3 targetPos = controller.transform.position + new Vector3(0f, 0f, 0f);
    transform.position = Vector3.Lerp(oldPos, targetPos, moveSpeed * Time.deltaTime);
    if(Vector3.Distance(transform.position, targetPos) > distanceLimit)
      transform.position = targetPos;
    oldPos = transform.position;

In the above code, you’ll notice the two log lines. In both the case where this script is working and the case where the script isn’t working, both logs show up in the console. This told me that the editor is (probably) successfully loading the script even though it doesn’t seem to be doing anything. It is also worth noting that there are no errors/warnings that are popping up in my console.

My last ditch solution to this would probably be to add the script component to the GameObject programmatically at start… I just hesitate to do this because it seems like such a bad solution.

Any and all help on this issue would be GREATLY appreciated, and I am more than happy to provide any other details that would help solve this problem.

Thank you!

Hey @Ein_Harrison,

Is seems to me that there is some corruption with your scene save.

Could you try this and let me know if it works please;

  1. Create a new scene
  2. Add the object that is failing the initialisation.
  3. Save the scene and project.
  4. Restart Unity
  5. Validate

This is definitely not a code issue, from the looks of things your code works as intended (considering re-adding the component is successful)

If there really is no way around it and is a Unity bug, the following should (not tested) resolve.

In any one of your other scripts that live on the same Object, class level add this:

public class SomeOtherScript: MonoBehaviour


I’ve solved the problem with some help from the Unity forums! The issue came down to my Unity project’s Script Execution Order. If anyone comes across this problem in the future, here is how I solved it:

  • Open Project Settings
  • Open “Script Execution Order” tab
  • Used the plus button to add my three movement-related scripts
  • Reordered the newly added scripts (see attached image)
  • Saved and restarted Unity, problem solved!