- Home /
Script doesn't work on duplicate objects. Please help
So I created a zombie with an EnemyController Script and after I finished everything, I duplicated it but the problem is that on the duplicate, the void Start() doesn't work.
void Start()
{
player = characterList.GetComponent<keepCharacter>().activeCharacter.transform;
hp.SetMaxHealth(60);
}
The first one can find the character's position and has his health bar full, but the duplicate does not. Please help.
Edit: I know that the function Update is being called because if I manually reference my player position(the reference should happen in Start function) the enemy starts moving (this happens in Update)
Is the function being called at all for the duplicate? Put a Debug.Log at the beginning of the start function and make sure the duplicate is at least calling the function. Are you getting any errors? Are you sure that your references are seeing the correct object instances (does hp point tot the correct object)? Where is activeCharacter being initialized? Is it in the start function of the keepCharacter script?
The Start function is not called at all by the duplicate. The Update function is called tho. I don't see any error and the references are all set correct. activeCharacter is initialized in the Start function of the keepCharacter. I even tried to put the script on a cube and reference my objects but it doesn't call the Start function.
the function should get called, it does inherit from monobehaviour right? Now if you have the player instanite in Start as well, then it might be instantiated after ehis script runs. Try instantiating the playing in Awake()
not working doesn't give a good description of what is going on. Also you will have to show more script and maybe scene setup to get help. I can't see anything wrong from just your start method.
What code do you use to "duplicate" the zombie? If instantiated properly Start will be called once for every enabled $$anonymous$$onoBehaviour on an active GameObject. If - as you say - Update is being called, Start should also be called, but in my experience not necessarily before the first call to Update. If execution order is the issue, try using Awake ins$$anonymous$$d of Start.
Answer by denisghera · Feb 25, 2020 at 03:42 PM
Finally, trying to find the problem, I changed the Start function in keepCharacter to an Awake function and now everything is fine.void Awake() { index = PlayerPrefs.GetInt("CharacterSelected"); if (index == 1) { c1.SetActive(true); activeCharacter = cc1; } if (index == 2) { c2.SetActive(true); activeCharacter = cc2; } }