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 IgorAherne · Dec 30, 2013 at 08:37 PM · variablestringnamestore

transform.name value to a variable

might be a little funny, but how do I store the string value from a gameObject.name into a string-type var?

ex:

string _name = transform.name // throws an error, because I cannot establish a link to tweak the name variable, and I don't want to - I only need the value xD

A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'

Sorry if this is too simple :D

Here is the code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class NodeSpark : MonoBehaviour {
 
     string string_name = gameObject.name; //throws the previously described exception
     
 }//end of class
 
Comment
Add comment · Show 12
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 azmat786n · Dec 30, 2013 at 08:41 PM 0
Share

not understand what do you want to do :(

 //C#
 string string_name = gameObject.name;
 
 //JS
 var string_name : String = gmaeObject.name
avatar image IgorAherne · Dec 30, 2013 at 08:46 PM 0
Share

that's the trick) with your example, you are establishing a link to the "name variable", you aren't copying it's variable.

Later on, if u change string_name's variable, it implies that you change the gameObject.name's value.

However, I only want to say that string_name becomes equal to the name's value

I've also updated the code, thank you for helping!

avatar image Spinnernicholas · Dec 30, 2013 at 09:47 PM 0
Share

People don't even read the question. Yes, it is bad to assign a variable in the body of a class. But the question is not, what is wrong with this script? It is how do I get a copy of a string as opposed to a reference?

avatar image Eric5h5 · Dec 30, 2013 at 09:55 PM 0
Share

You always get a copy of the string. Strings are by value, not reference. The correct answer is to declare the variable outside functions, and assign the value inside Start or Awake. And yes, that's what the question is asking....

avatar image Spinnernicholas · Dec 30, 2013 at 10:05 PM 0
Share

$$anonymous$$SDN on string says it's a reference type.

Show more comments

2 Replies

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

Answer by potatoking · Dec 30, 2013 at 09:34 PM

You can only do that inside of a function, so you could do that in the Start or Awake functions.

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 IgorAherne · Dec 31, 2013 at 09:42 AM 0
Share

not only that))

It can also be done OnEnable using Editor

avatar image
3

Answer by Eric5h5 · Dec 30, 2013 at 09:00 PM

Don't try to run code outside functions. Only declare variables outside functions; use Start or Awake to run initialization code.

 // Declare variable only; run code that assigns a value in a function
 string string_name;
Comment
Add comment · Show 6 · 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 Spinnernicholas · Dec 30, 2013 at 09:47 PM 0
Share

This doesn't answer his question.

avatar image Eric5h5 · Dec 30, 2013 at 09:56 PM 0
Share

Sure it does.

avatar image Spinnernicholas · Dec 30, 2013 at 11:36 PM 0
Share

Accept Eric5h5's answer.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
  
 public class NodeSpark : $$anonymous$$onoBehaviour
 {
   string string_name; 
 
   Awake()
   {
     string_name = gameObject.name;
   }
 }
avatar image Bunny83 · Dec 30, 2013 at 11:45 PM 0
Share

@"Whoever downvoted this answer": Do you "think" or "feel" the answer is wrong or do you know it is wrong? Because it isn't.

If you see misinformation, vote it down. Insert comments indicating what, specifically, is wrong

(FAQ)

We're still waiting for a clarifying comment :)

avatar image Eric5h5 · Dec 31, 2013 at 12:27 AM 1
Share

Well, I think potatoking's answer should be accepted since he was apparently first, but stuck in the moderation queue...if I'd checked the queue first I wouldn't have posted my answer since it's basically just what he posted.

Show more comments

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

23 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

Related Questions

String as Variable name 0 Answers

Is there a way to get the symbolic String name [editing name] of a variable at runtime? 1 Answer

Converting String to Variable Name 3 Answers

How can I store each axis of a transform's rotation as a separate variable? 2 Answers

Variable in String 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