x


Get script variable from collider

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!

more ▼

asked Aug 21 '12 at 03:37 PM

Random username gravatar image

Random username
176 4 7 14

Sorry for not explaining clearly. What I want is something like this:

GameObject kObject;

void OnTriggerEnter(Collider collider)
{
    kObject = collider.gameObject.getComponent(Script).xObject;
}

xObject would be an instantiated GameObject variable in the script attached to the gameObject which in this case would be the collider.

Aug 21 '12 at 04:06 PM Random username

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).

Aug 21 '12 at 04:12 PM Khada
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Your question is unclear, you want to store the object that triggers the OnTriggerEnter?

EDITED in response to feedback

GameObject kObject;

void OnTriggerEnter(Collider collider)
{
    kObject = collider.gameObject.GetComponent<ComponentType>().variable;
}
more ▼

answered Aug 21 '12 at 03:57 PM

Khada gravatar image

Khada
2.2k 3 9

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.

collider.GetComponent<ComponentType>()
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)
10|3000 characters needed characters left

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:

//This class keeps track of the time it exists as it is passed around objects.
public class TimeBaton : MonoBehaviour {

    public float time = 0;

    void Update() {
        time += Time.deltaTime;
    }

    //Call this method when the baton is passed from one object to another
    public void BatonPass(GameObject go) {
        //create new instance
        TimeBaton child = go.AddComponent<TimeBaton>();
        //copy var
        child.time = this.time;
        //destroy original
        Destroy(this);
    }
}
more ▼

answered Aug 21 '12 at 04:11 PM

gegc gravatar image

gegc
168 3 4 6

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:

gameObject....

you actually use:

this.gameObject....

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:

collider.gameObject....

You get the owning GameObject of that collider.

Aug 21 '12 at 04:46 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x356
x49

asked: Aug 21 '12 at 03:37 PM

Seen: 720 times

Last Updated: Aug 21 '12 at 04:46 PM