Script is losing component on runtime
I have script that controls animator. When I press play script loses component attachment. How to repair it?
Explain better please. Your gameObject with your script is losing the script or your variables assigned on the inspector on your script lose they references ? And can you share code and tell us what objects with what scripts have you ?
Code is pretty simple. I think it is correct. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class dip : $$anonymous$$onoBehaviour { public Animator anim; public static bool dips=false; double str1=pushupscenecont.str; public static double trudnosc; public static double bonussily=0.2; // Use this for initialization void Start () { anim = GetComponent(); }
// Update is called once per frame
void Update()
{
trudnosc = 70 / siła.sila;
double str1=siła.sila;
if (str1 >= 1 && energia.energy > trudnosc) {
dips = true;
} else {
dips = false;
}
}
void On$$anonymous$$ouseDown()
{
if(dips==true)
{
anim.Play ("dips");
energia.energy -= trudnosc;
siła.sila += bonussily;
bonussily = bonussily * 0.97;
}
}
}
I attach this script to button and attach an animator. In my scene I have 6 buttons that play other animation, but the code looks almost the same as this one. I will attach ss; After I press play button my script lose animator component. It looks like this;
Answer by unidad2pete · Aug 20, 2017 at 10:44 PM
Your code:
void Start () { anim = GetComponent(); }
Is setting the anim to heerself component, try delete that code: If not works when deletes the code, change to this:
void Start ()
{
anim = GameObject.Find("postac (1)").gameObject.GetComponent<Animator>();
} // Make sure the name is exactly like the object in scene with the animator