Generating a String via a Variable and Storing it Globally Multiple Times Without Overwriting the Previously Generated String

Hi there! I know a decent amount of C# and scripting in unity, but I’m still a bit new to it all.

How the Game Works:

So the game that i am creating is essentially a Social Media Simulator type of thing (Channel Simulator is the working title) where players will create content and upload it to the internet. The players create content by choosing a category, then using some method to choose how much time is spent on each part of the content (i.e. videos quality, sound quality, etc. (more to be unlocked as the game progresses)). Based on their choices, the game will calculate a percent (number between 0 and 1). I have all of this pretty much figure out, but what i don’t have figured out is the process after their content is created.

What I’m Wanting to do:

After the players content is created, he/she will begin to receive feedback via comments, views, likes/dislike, etc. Here is the script I have created that will generate a positive comment (I know this isn’t the most efficient way of doing things):

using UnityEngine;
using System.Collections;

public class PositiveCommentsGenerator : MonoBehaviour {

    public static string[] comments;

    // Use this for initialization
    void Start () {
        int comment = Random.Range(0, 20);

        string[] comments = new string[50];
        comments[0] = "i laughed so hard my abs hurt! Haha.";
        comments[1] = "I'm looking forward to your next video!";
        comments[2] = "cool vid!";
        comments[3] = "Hey wutz up guyz!";
        comments[4] = "I love it!";
        comments[5] = "Nice video and inspiring...keep up the good work and hey...well done";
        comments[6] = "Truly Awesome and Completely Amazing Video....";
        comments[7] = "very cool video";
        comments[8] = "Very nice video! Keep it up!";
        comments[9] = "Hello, how’s it going? Just shared this post with a colleague, we had a good laugh.";
        comments[10] = "I really enjoyed this! keep up the great work, :)";
        comments[11] = "Shared this with my friends!";
        comments[12] = "I just want to say this video si great.";
        comments[13] = "You're so inspiring";
        comments[14] = "I look foward to these everyday.";
        comments[15] = "I really like the fact that you put so much effort into this video";
        comments[16] = "Your content is my favorite!";
        comments[17] = "your vids really help me every day!";
        comments[18] = "your videos really entertain me :D";
        comments[19] = "i listen to your videos when I go to sleep";

        Debug.Log(comments[comment]);
    }

    // Update is called once per frame
    void Update () {
        
    }
    
}

I have this script attached to a Prefab, and when i need to generate a comment, I instantiate that Prefab, thus generating a comment. My question is, how can I store this newly generated comment globally so that the next time the Comment generator is run, it doesn’t override the previous comment? Sorry if I’m not explaining this very well, but any and all suggestions are very much appreciated!

-Serentix

I think I understand what you’re asking?

Instead of an array here, I’d recommend using a List. You can add and remove values easily, without allocating space. Plus, there are built in functions that make things easier, like sorting, etc. Here is a little example:

// create a list of strings
public static List<string> comments = new List<string>()
{
     "i laughed so hard my abs hurt! Haha.",
     "I'm looking forward to your next video!",
     "cool vid!",
     "Hey wutz up guyz!",
     // etc
};

Since it is static, you should be able to access it from any other script, like so:

// print out the third comment
Debug.Log(PositiveCommentsGenerator.comments[2]);

And because you don’t initialize it in Start, you don’t actually need it in the game scene! If you’re worried that the comment will be overwritten, an easy fix would be to check if the index value has been changed or not.

I hope this helps! :slight_smile:

P.S. If you do have it in the scene, and initialize the comments at run-time, if you try and get a comment from another script at Start, you’ll likely get an index out of range exception…