D
D
Daniel Tsedik2020-11-10 18:33:51
Unity
Daniel Tsedik, 2020-11-10 18:33:51

Unity object reference given to script in Editor not saved?

In short:
I have a checkpoint script (1) and a script that saves certain values ​​(2). From 1, the respawn coordinates come to 2. In the checkpoint in the inspector, I referred to 2, but on startup the link flies, and None is written in the link to 2 column (Bottom of the photo).

Now in more detail:

Scripts:

using UnityEngine;
public class checkpointScr : MonoBehaviour
{
    private bool opened = false;
    //data это то, что нам нужно
    public dataScript data;
    public BoxCollider2D mycollider;
    public PolygonCollider2D playercollider;
    public SpriteRenderer spriteRenderer;
    public Sprite newSprite;
    public Light Light1;
    public Light Light2;
    AudioSource mySounding;
    public AudioClip mySound;
    void Start()
    {
        data = GetComponent<dataScript>();
        mySounding = GetComponent<AudioSource>();
        Light1.intensity = 0;
        Light2.intensity = 0;
    }

    void Update()
    {
        if (mycollider.IsTouching(playercollider) && opened == false)
        {
            Light1.intensity = 1;
            Light2.intensity = 1;
            opened = true;
            mySounding.PlayOneShot(mySound, 0.9f);
            spriteRenderer.sprite = newSprite;
            //здесь скрипт передаёт координаты
            data.x = gameObject.transform.position.x;
            data.y = gameObject.transform.position.y - 1.6f;
            data.z = gameObject.transform.position.z;
        }
    }

}

Here is code 2:

using System.Collections.Generic;
using UnityEngine;

public class dataScript : MonoBehaviour
{
    //тут совсем пусто
    public float x = -3.01f, y = -1.73f, z = 0;
}



What I used to fix the problem:
  • Called public dataScript differently
  • Restarted Unity
  • Used a copy of the checkpoint
  • Reconnected the script to the checkpoint
  • Used the script on another object (same result)

But on the other hand, I can do it in another script using the same lines without any problems. So I can reconnect 2 to the script after launch and everything works fine.

Before launch:
5faab2bb7296a833266335.png

After launch:
5faab2d13f5d1337336385.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question