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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by V4mpy · Oct 16, 2013 at 09:48 AM · c#triggermanagerdialog

How to get variables from other Scripts on trigger enter?

Hello,

in my project, I have a DialogManager. This Manager can handle a Dialog parsed by a XML-Reader. This works fine. But now, I have more than one dialog. My idea (or my question): Get the name, or id of this NPC, if the player collides with them.

Process: Player collides with NPC A - press 'E' and a dialog (specified to NPC A) appears. The Player moves forward and collides with NPC B - press 'E' and a other(!) dialog appears.

Am I correct in this way?

Structure:

Player has a script 'DialogManger.cs' NPCs have a script 'ShowName.cs' - there is a variable 'NPCName' - this variable should be used for open different dialogs.

Thanks for reading.

Greetings,

V4mpy

Comment
Add comment
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

1 Reply

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

Answer by mattssonon · Oct 16, 2013 at 11:12 AM

You could get a hold of the name like this:

 void OnTriggerEnter(Collider other) {
     string name = other.gameObject.GetComponent<ShowName>().NPCName;
 }

If you make name global, e.g. declare it outside of the function, you could check which name is currently set when the 'E' key is pressed to show the proper dialogue.

Comment
Add comment · Show 6 · 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 V4mpy · Oct 16, 2013 at 12:18 PM 0
Share

Thanks for your reply. This solution sounds right and easy. But seems not to work or i am doing it wrong.

 ShowName npc = other.gameObject.GetComponent<ShowName>();
         
         if(!npc)
             Debug.Log("you are wrong here...");
         
         if(npc)
             Debug.Log(npc.NPCName);

I tried this.

ShowName is on my NPCs and my Player. Is that necessary?

Btw: Same Exception.

avatar image mattssonon · Oct 16, 2013 at 12:25 PM 0
Share

You tried it, but what happens? And no, ShowName does not have to be on Player.

avatar image V4mpy · Oct 16, 2013 at 12:38 PM 0
Share

Sorry, this "Btw: Same Exception" respective to my problem. In this code above, unity display all the time "you are wrong here...".

For my understanding:

A gameobject (other) collides (in 'OnTriggerEnter') with the player. Than, my code should get the component ShowName(Script). Is that correct?

avatar image mattssonon · Oct 16, 2013 at 12:43 PM 0
Share
 void OnTriggerEnter(Collider other) {
     string name = other.gameObject.GetComponent<ShowName>().NPCName;
 }

This should be in a script on Player. The ShowName script should be on the NPC the Player collides with. And yes, when the NPC collides with Player, OnTriggerEnter is called in the script on the Player and other.gameObject is the NPC object.

avatar image V4mpy · Oct 16, 2013 at 12:52 PM 0
Share

Hm...you are right. THIS works fine. Also in my case. $$anonymous$$y problem was: I had a Cube (my NPC) and a DialogTrigger (empty gameobject), this trigger was subordinate of the NPC. So my NPC had 2 collider...it work's now. Big thanks for your reply, you opened my eyes.

Show more comments

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

15 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 avatar image avatar image avatar image avatar image

Related Questions

[C#] How to destroy a wall next to my trigger? 2 Answers

OnTriggerEnter() not called 1 Answer

Problems when i have multiple enemies on the same scene C# 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer


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