Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mrnee · Nov 12, 2012 at 08:24 PM · javascriptloopvariable name

create object and add increment number to it's name

is that possible?

var nick:String = "enter name";

 class Player{
   var nick:String;
   var score:int;
   function Player(n:String){
     this.nick = n;
     this.score = 0;
   }
 }

 function OnGUI (){
     nick = GUI.TextField(new Rect(10,10,150,20),nick);
     if (GUI.Button (Rect(10,50,100,30),"add player")){
         var i:int = 1;
         var Player[i] = new Player (nick); // should be the same as var Player1
         i++; //number increments with each player added            
     }
 
 
     if (GUI.Button (Rect(130,50,100,30), "Start")) {
         Application.LoadLevel(2);
     }
 }

as the result i need to get Player1, Player2... Playern

also, how do i get these objects accessible in another scrip? static won't help here, will it?

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 mrnee · Nov 12, 2012 at 08:09 PM 0
Share

as fot the last question, appearantly public class should do the trick. right?

loading a new scene won't discard objects created in previous one?

sorry, strated learning javascript just a bit more than a month ago. been in unity for 1 week - so be patient with me :)

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by FakeBerenger · Nov 12, 2012 at 11:25 PM

I think you're a bit a confused between the type string, which is a value you can affect to a variable, and the name of that variable which is the point to your compiler what is going on.

Anyway, you can't write that as Player is a type, not the name of a variable. Furthermore, even with a variable, you can't declare arrays like that.

 var Player[i] = new Player (nick + i);

So, first, declare a variable playerCount (makes more sens than i) and an array of Player, outside of the scope of a function. With C#, it would have been more obvious what scope you're in when outside a function, but nevermind.

 var playerCount : int = 0;
 var players : Player[]; // That's how you declare an array in JS right ?

Then, you need to initialize that array in your Start or Awake function.

 function Start()
 {
     // I'm not used to JS, not sure that's the right way
     players = new Player[10]; // I set player max to 10, you can change it. There is other more convenient ways, but no need to complicate things.
 }

Finally, when you press the button, create a new player and increment the counter :

 players[ playerCount++ ] = new Player(nick); // I didn't check for out of range, be careful !
Comment
Add comment · Show 2 · 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
avatar image Seth-Bergman · Nov 13, 2012 at 06:53 AM 0
Share

correct on all counts :)

(WELL, your var changed from "playerCount" to "playerCounter", but that's beside the point..)

also, I'm going to give you the benefit of the doubt on the ORDER OF PRECEDENC$$anonymous$$.

avatar image mrnee · Nov 13, 2012 at 08:55 AM 0
Share

Sorry, I used Player[i] for ilustration purposes only. didn't know how to explain. Probably should have used "Player"+i.ToString() ins$$anonymous$$d.

Anyway, there are many ways to skin a cat and $$anonymous$$e also solved some problems i knew were ahead of me.

avatar image
0

Answer by JamieFristrom · Nov 12, 2012 at 09:24 PM

The problem here is that i is local to the if statement. If you declare i (maybe give it a better name, too) outside of OnGui() , and add it to nick, it should work. I didn't test this but in theory:

 var i:int = 1;
 
 function OnGUI (){
     nick = GUI.TextField(new Rect(10,10,150,20),nick);
     if (GUI.Button (Rect(10,50,100,30),"add player")){
        var Player[i] = new Player (nick + i); // should be the same as var Player1
        i++; //number increments with each player added         
     }
Comment
Add comment · Show 5 · 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
avatar image mrnee · Nov 12, 2012 at 11:10 PM 0
Share

I see your point - "i" will be restarted with every frame.

But still that doesn't solve the main problem. Nick is an argument that is passed to a function set one of parameters within the object. following your solution will result in

var Player = { nick : John1, score : 0, }

but i need to add 1 to 'Player' not 'John'

I remeber using something like var window("Player"+i) = new Player (nick);

but that doesn't work in unityScript

avatar image JamieFristrom · Nov 13, 2012 at 12:32 AM 0
Share

Ah, yes. Unity javascript is different from web javascript and takes some getting used to - one of the reasons I prefer C#. You want to be able to, after you've written the above code, access players with window.Player0 window.Player1 etc - but Unity javascript doesn't let you do that. I'd argue you don't really want to do that, anyhow - it's too easy to introduce bugs (whoops, misspelled the name or changed the case). If you do what FakeBerenger suggests you'll find you can easily iterate through the entire array of players ins$$anonymous$$d of having to copy-and-paste lines of code.

avatar image mrnee · Nov 13, 2012 at 06:37 AM 0
Share

last night I came across name+="Remote" which changes the name of an object to indicate whether the game object was created by you or remote player (this is for other project, this one is a hotseat).

it was 1:20am, so i didn't check it out, but i dont see any reason why var Player = new Player (nick); Player.name+=i.ToString(); wouldn't do exactly what i need.

P.S.: originally, my code had variable name ins$$anonymous$$d of nick, but it called my player 'main camera' by default, so i changed it... i should have guessed it sooner

avatar image mrnee · Nov 13, 2012 at 07:24 AM 0
Share

Ok, wrong again :( It appears, that name returns the name of physical object that script is attachet to.

Does everything in Unity has to be attached to a physical (including empty) object in 3d space?

This is far from original idea and a question, but i can create a GUI object for every player created which will show the name and score parameters.

Thinking out loud here

avatar image mrnee · Nov 13, 2012 at 08:45 AM 0
Share

IT IS ALIVE!!! $$anonymous$$UAHAHAHAHAHAAAA!!!!

Also figured out this piece of code in the process: guiText.text = gameObject.transform.parent.GetComponent("PlayerAttributes").score.ToString();

$$anonymous$$ust be no biggie for most of you, but it makes me fell like next most brilliant thing since sliced bread!

avatar image
0

Answer by JamieFristrom · Nov 13, 2012 at 12:36 AM

Oh, and to answer the second part of your question, how to access and whether to use static:

http://unitygems.com/mistakes1/

Comment
Add comment · Show 2 · 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
avatar image mrnee · Nov 13, 2012 at 06:42 AM 0
Share

Thanks a lot!

avatar image shieldgenerator7 · Dec 23, 2020 at 10:26 AM 0
Share

the link is broken :/

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

13 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

Related Questions

How to stop a for loop before it completes it's full cycle? 1 Answer

Can't get a sound to loop 2 Answers

Finding nearest object with a certain tag without for loops. 1 Answer

Iterate through Generic Dictionary? 3 Answers

Getting an Objects position once 2 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