public class Enemigo : MonoBehaviour
{
[Header("Scripts")]
public ManagerAjustes ManagerAjustesScript;
void Start()
{
ManagerAjustesScript = GetComponent<ManagerAjustes>();
}
}
I have the following problem, when trying to execute that line of code in a prefab it does not execute correctly, that is, the script does not put you in the prefab.
I tried with both Awake, Start and Update, but it still doesn’t work for me.
“Enemigo” and “ManagerAjustes” are in different objects.
since “Enemigo” is instantiated from a spawn, which the idea is to access “ManagerAjustes” every time a new enemy is instantiated
You’re going to have to provide more info than, since what I told you is correct so I have no idea what you’ve got setup incorrectly. But since you also said prefab, the object must be in the scene. You’re not going to access the script on a prefab.
Unity doesn’t call messages on the prefabs themselves so you should never be “put in the prefab” execution wise (there is ways around this, but you shouldn’t be doing that).
The prefab is just supposed to be the thing that gets cloned.
So this would find the first instance of ManagerAjustes that exists. Which may or may not be what you need.
Honestly… I have no idea what you’re trying to actually do. But if that meets your needs, welp, I guess it does. Though I can’t guarantee it’ll continue working for you based on the fact I don’t know if it actually does what you want, or it is accidentally behaving in this specific moment in a way that gives you a result you think you want.
It’s kind of like if you needed a quarter and you happened to find a quarter on the ground. Just because you found a quarter doesn’t mean quarters are always found on the ground.
I’m curious if you actually understand what FindObjectOfType does vs calling GetComponent on another gameobject. Because if you don’t understand it, you’re probably going to run into issues in the future.