- Home /
Get Script from gameobject knowing only parent script
So, I have an array of gameobjects which all has a unique script with only having the same parent script in common. I need to access the script in the gameobject to use a function. The gameobjects will only contain 1 script. Example:
Parent script:
public class Pet : MonoBehaviour {
...
}
Child script:
public class Bird : Pet {
...
}
Answer by hbalint1 · Apr 13, 2015 at 08:06 PM
I don't understand your question for 100%. But if you would like top reach a function in your base class (Pet), then you should just make it public. Like:
public class Pet : MonoBehaviour {
public void TheFunctionYouNeed() {}
}
Now you can access it through the instantiated object. For example on a bird:
GameObject.Find("Duck").GetComponent<Bird>().TheFunctionYouNeed();
If you want different behaviour on the "TheFunctionYouNeed" function in each derived class, just make it virtual in the Pet class and override it in each class.
Your answer
Follow this Question
Related Questions
Array of Arrays of GameObjects/Prefabs (C#) 1 Answer
Prefab not loading in data from Inspector 1 Answer
gameObject are not referenced 2 Answers
How to prevent prefab of custom editor inspector from overriding array size. 1 Answer
How to associate underlying data structure to prefab at runtime? 1 Answer