- Home /
How to access a variable for a Component of a different GameObject ?
Object A has a component as follows :
GameObject.Find("RadiusSpawner").GetComponent<InstantiateRadius>().radiusPrefab.radiusPrefab.collider2D.enabled = false;
which I wish to access from a script on Object B. What do I have to do to declare the same in the script for Object B ?
the same? I'm confused, if this is from the script attached to ObjectA, then you find "RaidusSpawner"... the same would apply for ObjectB? Can you explain a bit further?
I wish to access the collider2d component of Object A from Object B. I need it to manipulate the collisions for Object B. Thanks !
Why not just call the collider directly?
GameObject.Find("RadiusSpawner").GetComponent(Collider2D)()
For some reason it wont let me add triangle brackets so replace curved ones above.
Alternatively if you need to get an object from a script and change its collider you need to do an additional get component to find the collider.
Calling the collider directly worked alright. I dunno what got into me to call it through the script. Ackk ! Thanks mister ! $$anonymous$$indly post your comment as an answer so I can tag it Solved.
Answer by BitMax · Apr 18, 2014 at 09:13 PM
Just adding the answer :P
Why not just call the collider directly?
GameObject.Find("RadiusSpawner").GetComponent(Collider2D)()
For some reason it wont let me add triangle brackets so replace curved ones above.
Alternatively if you need to get an object from a script and change its collider you need to do an additional get component to find the collider.
Your answer
![](https://koobas.hobune.stream/wayback/20220613143032im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to compare variable component types between GameObjects at run time? 1 Answer
AddComponent with parameter variable 2 Answers
Passing a Script Name to a Function 2 Answers
How to set a variable equal to another variable in another gameobject? 2 Answers
Why can't I change another script's variable with this script? Thanks, 1 Answer