# Reference removed automatically

**URL:** https://discussions.unity.com/t/reference-removed-automatically/597264
**Category:** Scripting
**Tags:** Scripting
**Created:** [September 12, 2015, 11:43am UTC](https://discussions.unity.com/t/reference-removed-automatically/597264 "2015-09-12T11:43:43Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![phoda](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/phoda/32/960610_2.png) [@phoda](https://discussions.unity.com/u/phoda)
#### Post date: [September 12, 2015, 11:43am UTC](https://discussions.unity.com/t/reference-removed-automatically/597264/1 "2015-09-12T11:43:43Z")

</div>

I have references to scripts in Start() function like this

```csharp
sm = GameObject.FindGameObjectWithTag("SM").GetComponent<ScoreManager>();
        cm = GameObject.FindGameObjectWithTag("Camera").GetComponent<Cam>();

```

even for extra caution I added in Update() to check if it is null and if yes find them again but I have problem that it removes it’s refrence alone and it is null.

I think it forgets them after step it was referenced. After thoese references in start i added call to function to check buttons and it works, any time later if i call it again it just says null reference.

EDIT: also after first use of button.interactable it just says "  
MissingReferenceException: The object of type ‘Button’ has been destroyed but you are still trying to access it.  
Your script should either check if it is null or you should not destroy the object."  
while i clearly checked in inspector that it is active and not deleted and didnt change stored reference (it was drag&drop in inspector public reference)

---

<div class="post-metadata">

### Author: ![Jespertheend2](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jespertheend2/32/579943_2.png) [@Jespertheend2](https://discussions.unity.com/u/Jespertheend2)
#### Post date: [September 12, 2015, 1:40pm UTC](https://discussions.unity.com/t/reference-removed-automatically/597264/2 "2015-09-12T13:40:33Z")

</div>

right after your code in the Start() check if they actually get set.  
Do print(sm) and if it prints ‘null’ in the console they’re probably not even set in the first place.  
As far as I know references never really get removed unless you use Destroy() to remove the object or if you set it to null manually.

---

<div class="post-metadata">

### Author: ![phoda](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/phoda/32/960610_2.png) [@phoda](https://discussions.unity.com/u/phoda)
#### Post date: [September 12, 2015, 1:44pm UTC](https://discussions.unity.com/t/reference-removed-automatically/597264/3 "2015-09-12T13:44:59Z")

</div>

> [@Jespertheend2](#):
>
> right after your code in the Start() check if they actually get set.  
> Do print(sm) and if it prints ‘null’ in the console they’re probably not even set in the first place.  
> As far as I know references never really get removed unless you use Destroy() to remove the object or if you set it to null manually.

Yeah i know, its so weird, i even put print(sm); in update function and tells scoremanager is referenced but calling function from it returns null reference.

And yes i tried restarting computer 🙂

---

<div class="post-metadata">

### Author: ![Jespertheend2](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jespertheend2/32/579943_2.png) [@Jespertheend2](https://discussions.unity.com/u/Jespertheend2)
#### Post date: [September 12, 2015, 1:49pm UTC](https://discussions.unity.com/t/reference-removed-automatically/597264/4 "2015-09-12T13:49:59Z")

</div>

Well, in your edit you say you’re using button.interactable  
how exactly are you setting the ‘button’ variable?

---

<div class="post-metadata">

### Author: ![phoda](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/phoda/32/960610_2.png) [@phoda](https://discussions.unity.com/u/phoda)
#### Post date: [September 12, 2015, 2:08pm UTC](https://discussions.unity.com/t/reference-removed-automatically/597264/5 "2015-09-12T14:08:18Z")

</div>

for button it works in other part of script.  
it is initalized in start public Button turretbutton;  
then drag&drop button in inspectpor and it was working  
now i made some new updates to game and when i set interactable = false it does what it should but next time i try to set it to true says i destroyed object while in in-game inspector it is still there as it should be

---

<div class="post-metadata">

### Author: ![Jespertheend2](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/jespertheend2/32/579943_2.png) [@Jespertheend2](https://discussions.unity.com/u/Jespertheend2)
#### Post date: [September 12, 2015, 3:30pm UTC](https://discussions.unity.com/t/reference-removed-automatically/597264/6 "2015-09-12T15:30:10Z")

</div>

Hmm, it might be a bug with unity then, but honestly I’m not sure, I haven’t really played around with interactable yet.

---

<div class="post-metadata">

### Author: ![phoda](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/phoda/32/960610_2.png) [@phoda](https://discussions.unity.com/u/phoda)
#### Post date: [September 12, 2015, 3:39pm UTC](https://discussions.unity.com/t/reference-removed-automatically/597264/7 "2015-09-12T15:39:46Z")

</div>

Yeah, it’s really weird. I will post code later and SS.

---

<div class="post-metadata">

### Author: ![phoda](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/phoda/32/960610_2.png) [@phoda](https://discussions.unity.com/u/phoda)
#### Post date: [September 12, 2015, 6:09pm UTC](https://discussions.unity.com/t/reference-removed-automatically/597264/8 "2015-09-12T18:09:12Z")

</div>

Okay I tried some more solutions and found workaround but it still makes me think what is the problem.  
GM script:

```csharp
public ScoreManager sm;
    public Cam cm;

```

```csharp
void Start () {
        spawnnext = true;
        sm = GameObject.FindGameObjectWithTag("SM").GetComponent<ScoreManager>();
        cm = GameObject.FindGameObjectWithTag("Camera").GetComponent<Cam>();
        CheckButton();
    }

```

```csharp
void CheckButton()
    {
        if ((tpowerup > 1 || PlayerPrefs.GetInt(Cam.tokenKey) == 0) && freepower == false)
        {
            cm.DeactivateButton(0, 1);
        }
        else
        {
            cm.DeactivateButton(1, 1);
        }

        if (rpowerup > 1 || PlayerPrefs.GetInt(Cam.tokenKey) == 0)
        {
            cm.DeactivateButton(0, 0);
        }
        else
        {
            cm.DeactivateButton(1, 0);
        }
    }

```

And in update print it said cm was Cam script and when i press button and call this function it says null reference and then continues saying cm is Cam script which makes no sense.

Now i added

```csharp
cm = GameObject.FindGameObjectWithTag("Camera").GetComponent<Cam>();

```

every time i need to use cm and that is workaround but still, why is that problem happening?
