- Home /
,How to get the script of an element? or the GameObject of an Component?
Hi :) (see Picture) I have an GameObject 'PathA', which has a Component 'Script Instantiator'. This Scrit has Elements (Vehicles). Each Element has Component 'Script AICar'. How can I get the name of the GameObject 'PathA' from the Element?
I am coding in the Script of the Element. I want to say somthething like: this.transform.gameobject -> The parent Script -> It's parent GameObject, wich it is attached to.
Its actually the GetComponet Function the other way round, how can i code it?
,Hi :) I have a GameObject 'PathA', which has a Component 'Script'. The Script has a public List with Elements (Vehicles) in it. Each Element has the a Script 'AICar'. I want to find the name of the GameObject 'PathA' through my Elements. something like: this.transform.gameobject-> get it's Script > gehts its GameObject which it is attached to.
Answer by Hellium · May 19, 2019 at 10:38 AM
This is not possible the way you think. A gameObject / component does not know when it is referenced by another component.
Your only solution is to "inject" the reference of the gameObject / instantiator inside the vehicles
// Vehicle.cs
public Instantiator Instantiator
{
get ; set ;
}
// Instantiator.cs
private void Start()
{
for( int i = 0 ; i < vehicles.Count ; ++i )
vehicles[i].Instantiator = this ;
}
(Next time, think about doing a screenshot instead of taking a picture with your phone / camera)