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
0
Question by Alma · Jan 16, 2013 at 09:15 PM · objectscore

using objectname for scoring purposes?

Hello, I have many cubes on a plane - each one carrying a different score.

Each cube's name = its score.

I have problems scripting a score that will use the objectname as an int for an operation:

in particular this error "cannot implicitly convert type 'string' to 'int'" seems to be problem of a script that goes:

public class SCORE : MonoBehaviour {

 public static int score = 0 ;
 bool isHit = false;
 
 // Use this for initialization
 void Start () {
 }
 // Update is called once per frame
 void Update () {
 }
 void OnCollisionEnter(Collision Col) {

 if(isHit == false) {
 isHit = true;
     score = score + gameObject.name;

 

Script debugs OK using: score = score + 2;

Any suggestion?

Thank you!

Alma

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 robertbu · Jan 16, 2013 at 09:37 PM 0
Share

There are a couple of ways to convert a string to an int. Here is one:

int iOut = 0;

int.TryParse(gameObject.name, out iOut);

Note if TryParse() fails, iOut will remain unchanged. TryParse returns a bool if you want to check to see if the int conversion succeeded.

You could consider attaching a script to all of your objects that has a public int variable for score. You could get the name from the script ins$$anonymous$$d of from the name.

2 Replies

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

Answer by iwaldrop · Jan 16, 2013 at 10:32 PM

Changing the name of the GameObject isn't really the best way to store information. Why can't you just have an int in your Score script? It would be much cheaper to access ints directly than to constantly be creating new strings, setting gameObject's name, and parsing them into ints.

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
avatar image
0

Answer by Alma · Jan 17, 2013 at 07:10 PM

Thank you guys for your well appreciated suggestions!

Unfortunately, still no luck with my score...

I tried using TryParse but the script does not want to recognize Gameobject.name as an int - I fear that I am scripting it wrong somehow - I have never used it before, although I tried many different ways..

You see, I have around 15000 cubes - the 1 player being a sphere that moves around - aim of the game = score the max by hitting green high-value cubes.

13000 cubes have instead a "turn red if hit" script - they DO NOT matter "score-wise" 2000 cubes have a "turn green if hit" script - they matter "score-wise"

The 2000 cubes are all named individually with 2000 non-repeating numbers - which is why I am trying to use their name for scoring purposes as opposed to writing 2000 different scripts!

Well, should anyone have any other suggestion, that would be grand - in the meantime, I will continue trying with TryParse as it seems the best option so far!

Thank you,

Alma

P.S. --- each of the 2000 cubes has (on the surface to be hit) a GUI text named "XXX Text" and carrying text = "XXX" (XXX being a number representing the Cube's name/number) perhaps this can be of use for my scoring purposes instead of using the name of the cube?

Comment
Add comment · Show 1 · 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 DaveA · Jan 17, 2013 at 07:10 PM 0
Share

this should have been a comment. questions with more than one answer tend to get ignored.

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

11 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

Related Questions

creating score point for 3 objects 1 Answer

Trying to make Quizlike game. Click on object, get right or wrong response 0 Answers

Accessing multiple stats fields on Facebook 0 Answers

How do I Create 5 Random Racers? 2 Answers

How to store a class as a variable 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