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
1
Question by Asle · Mar 28, 2010 at 12:02 PM · instantiatevariableresetnamecounter

Problem naming instances with incrementally numbered counter

Hi

I have a variable count that keeps count of the objects i am creating and giving them a name based on it.

There is a small bug somewhere that makes it keep the last number it had from the last run of the script, alas it don't reset to 0;

var rate : float=0.0; var spawn : GameObject; var cube : GameObject; var count : int = 0; var position : Vector3; var text1="Press H for instructions."; var text2="Left mousebutton = Generate cubes"; var text = text1;

function Awake() { count=0; Debug.Log(count); }

function Update () { if (Input.GetButton("Fire1") && Time.time > rate) { position=spawn.transform.position; // check to see if spawnpoint is outside the room if (position.z <100 && position.z > -96){ if (position.x <100 && position.x>-100){ ++count; Instantiate (cube,position,Quaternion.identity); rate=0.25; count=count+1; cube.name=count.ToString(); } } } }

Comment
Add comment · Show 1
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 KvanteTore · Mar 28, 2010 at 12:21 PM 0
Share

So, it remembers count, is that it? Can you clarify what you mean by "the last run of the script"?

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by duck · Mar 28, 2010 at 12:43 PM

You're setting the name of the prefab rather than the name of the object you just instantiated. Changes to prefabs like this are saved with your project, therefore the next instance created of the prefab (even if it's after you stopped and replayed your app) comes out with the new name.

Also it seems as though you're incrementing your 'count' variable twice, in two different ways (using ++count and also count=count+1). You just need one of these.

You need to change your code so that you get back the reference to the instance you just created, and rename that. Something like this:

var cubeInstance = Instantiate (cube,position,Quaternion.identity) as GameObject;
count++;
cubeInstance.name = "cube "+count;

(the last line also shows how you can combine a string and the count number so your objects are named "cube 1", "cube 2" etc)

Comment
Add comment · Share
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

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

No one has followed this question yet.

Related Questions

I have a variable that does not re-set on the closing of my program, the variable is modified on the trigger enter, why does it not re-set? 0 Answers

How to reset a variable? 1 Answer

Passing Object References Through Function 0 Answers

transform.name value to a variable 2 Answers

How to keep variable value 3 Answers


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