TextBox.Color C# logic not working for two assets at once
Hi, Very junior C# coder here. I borrowed some code from another answer I found on here regarding color change on text boxes, and I'm having some trouble with improving the logic, as I convert it to a method.
I have two sprites "enemy" and "player". The code instantiates a text box as a child of either sprite, as defined by the method input. To save on prefabs, and coding space I am trying to design logic that changes the color of the text based on which sprite it's parented to.
My original idea took a Color.color input into the method and that works, but I am calling this method a lot in my code, and I'm trying to save keystrokes (plus it's just more fun to write code to do jobs for you).
Code:
public void displayText(string stringInput, GameObject i) { //will display text as speech on either player or enemy. Take's a string to display and a GameObject to instantiate on. Vector3 _textposition = new Vector3(i.transform.position.x + 0.5f, i.transform.position.y + 2f, -1); GameObject tempTextBox = (GameObject)Instantiate(textBox, _textposition, Quaternion.identity); tempTextBox.transform.SetParent(i.transform); TextMesh theText = tempTextBox.transform.GetComponent<TextMesh>(); if (i = player) { theText.color = Color.blue; } else if(i = enemy) { theText.color = Color.red; } theText.text = stringInput; Destroy(tempTextBox, 1.0f); }
So my problem is that it doesn't work. All text instantiates as blue. If I change the 'else if' to an 'if' all text is red, so I know it's something to do with my logic. If anyone out there could point me in the right direction I would be very grateful. Also if you have an idea of a Unity or C# method I could use to help do the job I am very open to learning.
Very excited to be part of this amazing community, thank you in advance.