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 Wes 1 · Jan 11, 2011 at 03:35 PM · javascriptguiscore

Can I reference a gameObject in a script attached to a different gameObject?

I'll use an example to explain, this score counter script is attached to an empty gameObject;

var scoreNumber = 0;

function OnCollisionEnter (hit : Collision) { if(hit.gameObject.FindWithTag("enemy")) scoreNumber += 10; }

function OnGUI () { GUI.Label (Rect(50,50,80,50), "Score: " + scoreNumber); }

But I don't need that script to work for the empty gameObject, it needs to work for the payers laser. And if I attach it to the laser then of course the script dies when the laser does, and my GUI disappears. So what I'm wondering is if in the beggining of the collision script, if I could do something like

var scoreNumber = 0;

function OnCollisionEnter (hit : Collision) { if((gameObject.Name "Laser")hit.gameObject.FindWithTag("enemy")) scoreNumber += 10; }

function OnGUI () { GUI.Label (Rect(50,50,80,50), "Score: " + scoreNumber); }

Or if that's at all possible.

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

2 Replies

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

Answer by Statement · Jan 11, 2011 at 04:31 PM

Can I reference a gameObject in a script attached to a different gameObject?

Yes. You can access a GameObject of a script (assumed to be called "target" in this example), that is placed on another game object.

// Get some game object by any means, in this example we use GameObject.Find. var otherGameObject = GameObject.Find("Other");

// Obtain the script that hold a reference to the GameObject you want. var otherScript = (YourScript)otherGameObject.GetComponent("YourScript");

// Here you are. var target = otherScript.target;

For your concrete example shown to us, you could create the GUI on one game object and have the lasers update the gui. Note that I will simplify some of the code involving the hit test that is redundant.

function OnCollisionEnter (hit : Collision) 
{
    if (hitTarget)
    {
        var score = GameObject.Find("Score");
        var scoreScript = (ScoreScript)score.GetComponent("ScoreScript");
        scoreScript.total += 10;
    }
}

Another easy approach you can take is to have a static member that everyone can access directly.

function OnCollisionEnter (hit : Collision) 
{
    if (hitTarget)
    {
        // here, total is a static variable of ScoreScript.
        ScoreScript.total += 10;
    }
}

ScoreScript should then only have a variable that it uses to display score at all times, regardless if lasers are firing or not.

Comment
Add comment · Show 3 · 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 Wes 1 · Jan 12, 2011 at 06:28 AM 0
Share

Thanks so much, I'll be sure to work off this ASAP.

avatar image Traiger · Dec 27, 2013 at 05:05 AM 0
Share

Nice answer thanks for this, I found that the documentation reference for the function highlighted some limitations as well as feature such as navigating the object hierarchy:- http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html

avatar image Srbhunter · Feb 25, 2014 at 12:24 AM 0
Share

Thank you, nice answer!

avatar image
1

Answer by Jesse Anders · Jan 11, 2011 at 04:30 PM

Short answer: read this.

Slightly longer answer: there's pretty much always a way to access whatever game object, script, or variable you need to access; it's usually a simple matter of acquiring the necessary references using whatever method is most appropriate (the aforementioned link described several methods you can use).

Also note that this:

if(hit.gameObject.FindWithTag("enemy"))

Doesn't do what I'm guessing you think it does. I'm guessing you probably want this:

if(hit.gameObject.CompareTag("enemy"))

Also, this:

if((gameObject.Name "Laser")hit.gameObject.FindWithTag("enemy"))

Is both syntactically and semantically meaningless, as far as I can tell. If there's something in particular you're trying to accomplish here, you might try describing it in words rather than guessing at the code.

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

2 People are following this question.

avatar image avatar image

Related Questions

Point system help! 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Pause When Score Reached? 2 Answers

Need help displaying score (JavaScript) 1 Answer

GUI Label Score Overlapping - Javascript 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