call a col routine only once

Asked 1 years ago, Updated 1 years ago, 62 views

We are developing FPS in Unity 3D.

In the script below, when a player touches a character standing in a field, a routine starts and a message dialog appears.

[Problems Occurring]
Currently, when a player touches a character, the routine is executed only once, and it will not reappear after the second contact.

修正What I want to modify したい
I would like to loop the routine so that a dialog appears every time a player touches a character. What is the appropriate method?

Also, I would like to know why the routine only works once using OnCollisionEnter.

As a result of trial and error, I encircled yield return new WaitForSeconds (0.01f); in while(true), but it was meaningless.

I don't think void Update() is necessary because OnCollisionEnter() should run every time I come into contact with it. I've also considered the OnEnable() and OnDisable() methods, but I'm asking for advice on how to rewrite them.I looked for specific code writing information, but I couldn't find a topic similar to my problem, so it's hard to solve it by myself.

MessageScript.cs (script attached to characters to talk)

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class MessageScript: MonoBehavior
{
    SerializeField
    private MessageScript;// Load the Message script.
    public string [ ] message1; // Create as many conversations as you want to manage.
    public string [ ] message2;
    public string [ ] message3;

    public void start()
    {
      
    }


    void OnCollisionEnter (Collision col) 
    {
      if(col.gameObject.name=="Player")  
        {
            StartCoroutine("Message", message1); // Execute Message coroutine.
        }
    }
 
    IEnumerator Message (string[]Conversation) // Message Coroutine
    {

        yield return new WaitForSeconds (0.01f); // Wait 0.01second.
        messageScript.SetMessagePanel (Conversation); // Execute SetMessagePanel of messageScript

        
    }
}

Added script to control character lines.
Please let me know if there is any code that prevents you from re-running the routine.
Thank you for your cooperation.

Message.cs (script to control character conversation)

void SetMessage (string message) // SetMessage
    {
        This.message=message;// Make the message of this object a message
    }
 
    public void SetMessagePanel (string[]message) // SetMessagePanel
    {
        i = 0; // Set to 0
        stringsCount = message.Length; // Set the total number of lines in the string to the number of elements in the message

        conversation=message;// Make coverage a message.
        canvas.SetActive(true); // Display the canvas
        SetMessage(conversation[0]); // Execute SetMessage.
        canvas.transform.GetChild(1).gameObject.SetActive(true); // Display the child objects of the canvas

        isEndMessage=false;//Set isEndMessage to false.
    }

c# unity3d

2022-09-30 20:10

1 Answers

Japanese is not my native language, so please forgive me.
It's a very old question, so I don't know if I've reached the answer myself.
If you have any other questions, please let me know.

It says, "Cortin only did it once," but what is the basis for that?
From my point of view, it's just that the text you see doesn't change even if you do it over and over again.

To display different text each time, you should put them all in the List and pull them out one by one.

I can't see the entire Message.cs, so I'm not sure if the text will appear as I want it to.

example:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class MessageScript: MonoBehavior
{
    SerializeField
    private MessageScript;// Load the Message script.
    public string [ ] message1; // Create as many conversations as you want to manage.
    public string [ ] message2;
    public string [ ] message3;
    
    private List<string[]>messageList=new List<string[]>();
    private int index;
    

    public void start()
    {
       messageList.Add(message1);
       messageList.Add (message2);
       messageList.Add(message3);
    }


    void OnCollisionEnter (Collision col) 
    {
      if(col.gameObject.name=="Player")  
        {
            StartCoroutine ("Message", messageList [index]); // Execute Message coroutine.
            index=index+1;
        }
    }
 
    IEnumerator Message (string[]Conversation) // Message Coroutine
    {

        yield return new WaitForSeconds (0.01f); // Wait 0.01second.
        messageScript.SetMessagePanel (Conversation); // Execute SetMessagePanel of messageScript

        
    }
}


2022-09-30 20:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.