Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Random username · Aug 21, 2012 at 03:37 PM · variablesaccessing

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Random username · Aug 21, 2012 at 04:06 PM 0
Share

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.

avatar image Khada · Aug 21, 2012 at 04:12 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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;
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 21, 2012 at 04:30 PM 1
Share

$$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>()
avatar image Khada · Aug 21, 2012 at 04:33 PM 0
Share

Ah, true that. I have a habit of doing it that way though :P

avatar image Random username · Aug 21, 2012 at 04:36 PM 0
Share

Thanks a lot :)

avatar image Khada · Aug 21, 2012 at 04:38 PM 0
Share

You're welcome :)

avatar image
0

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);
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 21, 2012 at 04:46 PM 0
Share

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.

avatar image
0

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();
                     }
                 }
             }
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I change a variable in Script A from inside Script B 1 Answer

Accessing Other Script Variables (C#) 2 Answers

Adding variables from all scripts 2 Answers

How can I edit my static variable in the editor? 1 Answer

Access variables,method from a Mono Behaviour without static use. -1 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges