Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Topthink · Oct 02, 2017 at 08:26 PM · uilistclassdisplaycounter

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;
     }
 }
 
 



  
Comment
Add comment · Show 13
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Topthink · Oct 02, 2017 at 08:35 PM 0
Share

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).

avatar image Topthink · Oct 02, 2017 at 08:55 PM 0
Share

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 .

avatar image MacDx Topthink · Oct 02, 2017 at 09:22 PM 1
Share

@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.

avatar image Topthink MacDx · Oct 02, 2017 at 09:32 PM 0
Share

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.

Show more comments
avatar image Topthink · Oct 02, 2017 at 09:00 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges