When the ChangeLetter method is run, it works as expected a new letter from the array is seen in Debug.
using UnityEngine;
public class Tester : MonoBehaviour
{
string Letter;
string[] Letters = new string[5] { "d", "e", "x", "y", "z" };
string QuestionString;
public void ChangeLetter()
{
Letter = Letters[Random.Range(0, Letters.Length)];
QuestionString = Letter;
Debug.Log(QuestionString);
}
}
Problem:
When getting the question/strings format from a List of strings instead i get null.
using System.Collections.Generic;
using UnityEngine;
public class Tester : MonoBehaviour
{
string Letter;
string[] Letters = new string[5] { "d", "e", "x", "y", "z" };
string QuestionString;
public List<string> Questions = new List<string>();
private void Start()
{
SetQuestions();
}
public void ChangeLetter()
{
Letter = Letters[Random.Range(0, Letters.Length)];
QuestionString = Questions[0];
Debug.Log(QuestionString);
}
void SetQuestions()
{
Questions.Add(Letter);
Questions.Add(Letter + Letter);
Questions.Add(Letter + Letter + Letter);
}
}
Question:
I am trying to create a list of questions (strings) and have them vary as their variables are changed e.g. āLetter = Letters[Random.Range(0, Letters.Length)];ā - How is this done correctly please?
Reason for wanting the list of strings:
I want to use strings as a template for hundreds of questions, when a question is generated that template is pulled from the list in the desired place - this is to avoid generating each question over and over whilst only using one at a time.
Letter is an empty string right? You didnāt initialize it to anything in the declaration, so maybe the contents of the Questions list are null when you try to access it in ```ChangeLetter()
I donāt know how to get around it. I am wanting to use a list of strings as templates for sentences, these contain variables (e.g. Letter) then the relevant string is pulled from the list of strings, the variables are applied to it and they can change (e.g Letter = Letters[Random.Range(0, Letters.Length)]
I just need to know the correct way to do that. I can get the strings from the lists but I cannot get them to update when their variables are changed.
Thatās the key there. Strings are a value type (like int, float, etc), not a reference type (like most classes). That means that when you set a string to another string, the value is copied to the new one. So in a simple example:
string a = "a1";
string b = a;
a = "a2";
Debug.Log(b); // Prints "a1"
So if you want the āfull list of questionsā string to update when its component strings are changed, you need to rebuild it either every time itās accessed, or every time a component string changes.
I recommend googling C#'s StringBuilder class, which is designed to efficiently do exactly this kind of thing.
StarManta thank you so much for your response and your explanatory example (which I understand and further to that, understand what I misunderstood).
In trying to solve this, I have done so with Regex but (my use) is ugly but functional. I have tried string.Replace and I have come across StringBuilder and now that will be my focus. One of my main problems has been āI can solve this but I do not know which is the best way nor do I know the most efficient wayā and now I do so I canāt thank you enough! I wish Iād bumped into you earlier.
String is actually kinda special. Itās a reference type but with some unusual rules.
Your example code doesnāt really demonstrate anything; youād get the same result for both value types and reference types, because youāre just doing variable assignments.
SomeClass a = new SomeClass(1);
SomeClass b = a;
a = new SomeClass(2);
// b still refers to the object instantiated on line 1
// a now refers to an entirely new object instantiated on line 3
The difference comes in when you change something about the underlying object (not just the variable):
SomeClass A = new SomeClass();
A.someField = 3;
SomeClass B = A;
A.someField = 7;
Debug.Log(B.someField); // prints 7
// With reference types (like classes), B is an alias of A, so changes to A are reflected in B
// With value types (like structs), B would have been a copy of A, so changing A
// would have had no effect on B (and we'd print 3 instead of 7)
As for strings: in C#, strings are a reference type, but string objects are immutable, meaning that you canāt ever change them after theyāve been created. (Any function that looks like it is modifying a string is actually creating and returning a new string.) So examples like the one above where you change something about the object canāt even happen in the first place! In ordinary situations, you canāt actually tell from your codeās output whether strings are value types or reference types, because the operations that would let you tell the difference are illegal. (There are some āunder the coversā differences, though; mostly for performance.)
For simple substitutions, your easiest option is probably string.Format(), which lets you supply a ātemplateā that says where variables will get inserted.
Antistone, first of all thank you for your replies theyāre more valuable to me than you perhaps realise. If I for example had just seen this days ago you would have saved me double-digits of hours of frustration and confusion.
I want to be good at coding (in the context of unity/c# as a focus) not just āpassableā as Iām tired of running into roadblocks because I have built a poor foundation. I would like to be of a standard and understanding where I can break things down as you can and have here.
What do you think would be a good place/resource to start at/with?
I will do any course or work through block of work you recommend as a starting point. (I donāt want you to go out of your way and find me anything more justā¦if you were to direct yourself from the past where to start where would it be?)
Thank you for any advice and again for your insight here.
Iām afraid Iām not much good at recommending introductory materials for new programmers. I started programming when I was a kid, so I donāt have a good conception of what order you should learn things in, and the actual sources I used are generally both out-of-date and unavailable today.
The one point I usually hammer on for newbies is that computers are like evil genies: They give you exactly what you ask for, not what you meant. Computers do not have common sense and do not infer your end goal. Every piece of code that you type has a formal, precise, mathematically-rigorous meaning, and the computer is going to follow it to the letter (no matter how ridiculous the outcome).