|
Hey, I have an OnTriggerEnter method. I want to set a gameobject variable in my script to the gameobject variable of the script attached to the collider object. I was hoping I could do it something like this: GameObject x = collisionObj.GameObject.BroadcastMessage("addGameObject"); Where the addGameObject method returned a GameObject. Unfortunately that didn't work. I also tried x = collisionObj.getComponent("Script").y; Unfortunately that didn't work either :( How should I go about doing this? Thanks for your time!
(comments are locked)
|
|
Your question is unclear, you want to store the object that triggers the OnTriggerEnter? EDITED in response to feedback Keep in mind that every component has the same functions. You don't have to access the gameObject. Just use the GetComponent function of another component.
Aug 21 '12 at 04:30 PM
Bunny83
Ah, true that. I have a habit of doing it that way though :P
Aug 21 '12 at 04:33 PM
Khada
Thanks a lot :)
Aug 21 '12 at 04:36 PM
Random username
You're welcome :)
Aug 21 '12 at 04:38 PM
Khada
(comments are locked)
|
|
the variable "gameObject" of MonoBehaviour is always the GameObject to which the script is attached - afaik, you can't change it. From what I understand, you want to "reassign" a script from one GameObject to another. Well, I don't think you can do that. I would recommend instead making a "copy constructor" of sorts in the script you are trying to reassign, that would take as an argument a GameObject, add a copy of itself with AddComponent(), and then set all the local vars to their proper values (if you need this). Then call Destroy(this) to destroy the original script. Call this method in your OnEnterTrigger(). Example: I'm afraid but i thing you got thie question wrong ;) He don't want to reassign something. He just want to access a variable of a MonoBehaviour component that is attached to the colliding object. Also gameObject (which is a property, not a variable ;)) is a member of the Component class. So all components have this property to access the gameobject to which the component is attached to. Inside a MonoBehaviour (which is also a Component) when you write: you actually use: That means you access the gameObject variable of your own component. When you have a reference to another component (like in OnTriggerEnter where you get a reference to the collider that enters your trigger) and use: You get the owning GameObject of that collider.
Aug 21 '12 at 04:46 PM
Bunny83
(comments are locked)
|

Sorry for not explaining clearly. What I want is something like this:
xObject would be an instantiated GameObject variable in the script attached to the gameObject which in this case would be the collider.
I've edited my post to do what you want, you should post replies like this as comments though, not answers. (you can type code blocks in a reply box and then copy into a comment box).