how do I add link script to gameobject that already exist with attached script
Without creating a new gameobject and without specifying name of gameobject That already exist something like
ClassAnother nameofTheInstance = this.gameobject.GetComponent(ClassAnother);
ComponentClassName myComponentInstance = gameObject.GetComponent<ComponentClassName>();
as an alternative
internal ClassAnother nameofTheInstance;
then drag in inpector Script name to reference field
Answer by wideeyenow_unity · Jul 22, 2021 at 03:36 AM
adding another script to an object doesn't make sense. You can just call the script or it's functions by:
ScriptName other1;
Start() {
other1 = GetComponent<ScriptName>();
}
Update() {
if (other1.goldCoins == 17) { other1.goldCoins = 99; }
}
//short for ScriptName.GetComponent<ScriptName>().goldCoins;
Unless you're creating a new object, and need to add the script, then:
createdGameObject.AddComponent<ScriptName>();
i believe you mean to use AddComponent rather than GetComponent in the second part
Your answer
Follow this Question
Related Questions
Reference a script on a gameobject that is disabled in scene 0 Answers
Object reference; sometimes works but sometimes doesn't??? 2 Answers
this.GetComponent() won't return gameObject? How to get it without using this.gameObject? 3 Answers
How to convert an Addressable as Gameobject 1 Answer
access object from another 2 Answers