Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Tryptex · Mar 29, 2019 at 12:48 AM · unity 5scripting problemunityeditorarraysnoob

How to detect which how many of a type a variable a script has

I am trying to make NPC's that have a text box that displays a random canned message from a script. My only problem is that every NPC has this same script and each NPC has a different number of canned messages. So I need to write a script that can adapt to the number of canned messages to say that have been added to that individual script in the editor and pick random between those messages. Here is my script:

 public class BasicNPCPrototype : MonoBehaviour
 {
     private bool knowBefore;
     private int whichDialog;
     private DialogueManager dialogueManager;
 
     public int numberOfDialogues;
     public Dialogue firstMeet;
     public Dialogue dialogue1;
     public Dialogue dialogue2;
 
     // Use this for initialization
     void Start ()
     {
         dialogueManager = FindObjectOfType<DialogueManager>();
     }
     
     // Update is called once per frame
     void Update ()
     {
 
     }
 
     public void Speak()
     {
         // Choose what to say
         whichDialog = Random.Range(1, numberOfDialogues);
 
         // If this NPC has never been talked to before
         if(!knowBefore)
         {
             dialogueManager.StartDialogue(firstMeet);
             knowBefore = true;
         }
         else
         {
             // Call the "StartDialogue" function in the DialogueManager and pass in our dialogue variable
             dialogueManager.StartDialogue(dialogue1);
         }
     }
 }

I need some way of being able to change the number of Dialogue variables in the editor and have the computer look at the number, put it into an int, and use it in Random.Range. But my biggest challenge will be creating the Dialogue variables in the editor and getting the computer to see it. Thanks.

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

Answer by Bruno2907 · Mar 29, 2019 at 01:38 AM

Hi, if I understood your problem correctly, what you need to do is to use a dialog array and then get the number of dialog using [array-name].Length Here, try this:

 public class BasicNPCPrototype : MonoBehaviour
 {
     private bool knowBefore;
     private DialogueManager dialogueManager;
 
     public Dialogue firstMeet;
     public Dialogue[] dialogue;
 
     // Use this for initialization
     void Start ()
     {
         dialogueManager = FindObjectOfType<DialogueManager>();
     }
 
     // Update is called once per frame
     void Update ()
     {
 
     }
 
     public void Speak()
     {
         // If this NPC has never been talked to before
         if(!knowBefore)
         {
             dialogueManager.StartDialogue(firstMeet);
             knowBefore = true;
         }
         else
         {
             // Call the "StartDialogue" function in the DialogueManager and pass in our dialogue variable
             dialogueManager.StartDialogue(Random.Range(1, dialogue.Length));
         }
     }
 }
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 Tryptex · May 08, 2019 at 06:59 PM 0
Share

@Bruno2907 Hi, sorry for the late reply. It says it can't convert a Dialogue to int because the Dialogue$$anonymous$$anager is expecting a Dialogue variable that looks like this:

 [System.Serializable]
 public class Dialogue
 {
     public string name;
 
     [TextArea(3, 10)]
     public string[] sentences;
 }

So while you're on the right track I need a way of deciding which dialogue to send to the Dialogue$$anonymous$$anager and then getting that and sending it. You have provided me with a way to pick a random dialogue, can you show me how I would get the dialogue out of the other dialogues in the array to send it to the Dialogue$$anonymous$$anager?

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

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

Related Questions

How can I change ApiCompatibilityLevel before Unity start? 0 Answers

Program not run/ refresh in unity,Not Run Program 1 Answer

Polymorphism for decisions in a dialogue editor 1 Answer

check for click in rhythm game 0 Answers

how do I move array objects to world coordinates? 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