- Home /
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!
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.
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).
Answer by Khada · Aug 21, 2012 at 03:57 PM
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;
}
$$anonymous$$eep in $$anonymous$$d 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>()
Ah, true that. I have a habit of doing it that way though :P
Answer by gegc · Aug 21, 2012 at 04:11 PM
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);
}
}
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 $$anonymous$$onoBehaviour 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 $$anonymous$$onoBehaviour (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.
Answer by augivision · Mar 29, 2018 at 10:43 PM
I like to use tags. You can do getcomponent, getcomponentinparent, get component in children, and etc. Makes it easy.
Here are some different examples using touch.
void Update()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
Ray ray;
RaycastHit hit;
ray = Camera.main.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if (hit.transform.gameObject.name == ("HarmonyVideo"))
{
Application.OpenURL("https://www.harmonyextracts.com/");
}
if (hit.collider.tag == ("Ticket"))
{
gothicHandler = hit.collider.GetComponentInParent<GothicEventHandler>();
gothicHandler.OpenLink();
}
if (hit.collider.tag == ("GothicEvent"))
{
gothicHandler = hit.collider.GetComponent<GothicEventHandler>();
gothicHandler.ToggleActive();
}
if (hit.collider.tag == ("YouTubePlayer"))
{
youTubePlayer = hit.collider.GetComponent<YouTubePlayer>();
youTubePlayer.OpenYouTubeLink();
}
}
}
}
}