• 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 beatniq · May 10, 2014 at 06:44 AM · gameobjectstringpassing

How to pass string from one script to another in C#

I am need to know the value of a string located in a different scriptin C#. I need to get the string from a script called GameManager. The script is applied to a game object called _GameManager I have also attached a tag to the game object called _GameManger.

The string I need from the GameManger script is called suspectName. I need to pass it into a script called CharacterTile that is attached to a game object called CharacterTile and has the tag CharacterBox.

Hope this isn't to confusing.

Comment
Add comment · Show 2
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 · May 10, 2014 at 06:51 AM 0
Share

Getting access to other game objects is the most asked question on UA. A couple of links:

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

See the GetComponent sections here: http://unitygems.com/

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Components.html

https://www.youtube.com/watch?v=mtl8wpZec9Y

avatar image iamvishnusankar · May 10, 2014 at 07:03 AM 0
Share

Also you may check this question

http://answers.unity3d.com/questions/704289/how-to-make-an-central-script-with-viriables.html#comment-704295

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Erisat · May 10, 2014 at 07:30 AM

in CharacterTile:

string suspectName = "";

void Start()

{ //correction below, thanks to robertbu for pointing out mistake

GameManager gameManager = GameObject.Find("_GameManager").GetComponent("GameManager") as GameManager; suspectName = gameManager.SuspectName();

}

and then in GameManager declare:

public string SuspectName() { return suspectName }

hope this helps.

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 robertbu · May 10, 2014 at 07:33 AM 0
Share

Don't use a string parameter for GetComponent (plus it will generate a compiler error unless you cast things). Use the generic version for C#.

Note the OP did not indicate what language he was writing in.

avatar image Erisat · May 10, 2014 at 08:12 AM 0
Share

You are correct, I posted that without actually trying it. Correction is:

string suspectName = ""; void Start() { Game$$anonymous$$anager game$$anonymous$$anager = GameObject.Find("_Game$$anonymous$$anager").GetComponent("Game$$anonymous$$anager") as Game$$anonymous$$anager; suspectName = game$$anonymous$$anager.SuspectName(); }

(i tried it this time, it works. my apologies for earlier post)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

GetComponent() - Is it possible to pass a string variable as name of the script? 3 Answers

Creating an Object with a string C#! How? 1 Answer

Reference an object's name in a string? 1 Answer

Click GameObject to change Color 1 Answer

Tag is not defined, when it has been manually created? 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