- Home /
Why Is TextMesh Off by One ???
Code provided in its entirety below:
I create five Prefab Spheres in this program (a test for a much larger program). My "counter" begins at 1. Line 25 names each Sphere a Letter and then a number. The number is always the "counter" multiplied by 11. So the five Spheres would display their names as "A 11", "A 22", "A 33", "A 44", and "A 55"
... or so you would think. And, that is what happens if I run the program TWICE.
Say now that I change the letter "A" to a "B" and you would expect "B 11", "B 22", "B 33", "B 44", and "B 55" ... Right? (That is what I expect)
But, the first time I run it, the "B 55" remains an "A 55" ... Odd, if I run the program TWICE, then everything looks okay. But, the first time I run it fresh, it is always off by one in the respect that the name of the custom list item contains a name from the previous program run. Hmmmm.
Entire Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour
{
private int counter = 1;
public GameObject testSphere;
public TextMesh myName;
public static List<testCount> countStuff = new List<testCount> ();
void Start ()
{
for (int i = 0; i < 5; i++)
{
SpawnThing();
counter++;
}
}
void SpawnThing()
{
Instantiate (testSphere);
testSphere.transform.position = new Vector3 (Random.Range (.5f, 9.5f), .5f, Random.Range (.5f, 9.5f));
string tempString = "A " + (counter * 11).ToString (); /// /// /// /// /// Change This -- Everything doesn't Change ////////////////////////////
int tempInt = counter * 1111;
myName.text = tempString;
countStuff.Add(new testCount(tempString,tempInt));
}
}
public class testCount
{
public string thingName { get; set; }
public int thingID { get; set; }
public testCount(string n, int i)
{
thingName = n;
thingID = i;
}
}
Again, if I run it twice, everything looks fine. But, whenever I change any code for the name, it seems to keep one of the names from the last run/code.
Very odd.
I'm trying to figure if there is something wrong with the way I add the names and numbers to the custom class items but, it seems proper (obviously, it's not or it would work each time I change the code).
If I throw a Debug.Log( counter, tempName, tempID ) and change the letter to "C" then I get exactly what I think I should get. Which is :
1 C11 11111 2 C22 22222 3 C33 33333 4 C44 44444 5 C55 55555
HOWEVER...at the same time, what prints on the Spheres is C11, C22, C33, c44 and ... B55 .
@Topthink in your last post you have basically the same code, and on that post I commented and explained to you the errors that the code had, which are the very same errors this code has, now I'm not sure if you even read and tried what I suggested on the last post and that's making me a bit anxious. I'll post them here one last time, ok?
Error 1: You are modifying the game object you are using to spawn clones, NEVER do that
You have this field:
public GameObject testSphere;
Then you are doing this:
Instantiate (testSphere);
testSphere.transform.position = new Vector3 (Random.Range (.5f, 9.5f), .5f, Random.Range (.5f, 9.5f));
Never, you should never modify the same game object reference you are using to instantiate clones, what you should do is save the game object reference that the instantiate method returns and use that to modify the newly created object. Like this:
GameObject clone = Instantiate(testSphere);
clone.transform.position = WhateverValueYouWantHere;
You see? Don't change testSphere because testSphere is the template. Change the clone
Error 2: You expect every testSphere spawned to have different text but NOWHERE, inside your code do you ever modify each sphere's unique Text$$anonymous$$esh's text value, NOWHERE, Ok? You should have something like this inside your SpawnThing method.
//Taking into account the suggestion above
clone.GetComponent<Text$$anonymous$$esh>().text = tempString;
That way you are accessing each sphere's text mesh and not modifying myName multiple times ins$$anonymous$$d.
P.S. $$anonymous$$ake sure you are using a prefab object in your testSphere field. Prefab object means, you have an object on scene, you drag it to your assets folder, that automatically creates a file, then you drag that new object (The one in the assets folder, not the one on the scene) to your testSphere field.
So we meet again. :)
I'm looking at your comments now although it takes me awhile to figure things out sometimes.
Thanks (again) for taking the time to help me.
The object that the spheres are placed on is a tiny 10 x 10 terrain.
The error occurs no matter how many objects are placed on the terrain...the last item will be from the previous program run.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Trouble Getting Array List To Display Current Song In Game 0 Answers
List of Classes (js) - How to Construct Properly 1 Answer
Foreach - what am I doing wrong? 1 Answer
Problem with arrays in a list 1 Answer