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 bpears · Jan 08, 2013 at 10:04 PM · gameobject

Finding game object, not working

Trying to use variables of another script by using this: but it is not finding it. Am I supposed to include the .js?

var manager: WeaponManager=GameObject.Find("WeaponManagerScript");

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
1
Best Answer

Answer by Julien-Lynge · Jan 08, 2013 at 10:07 PM

Your code is trying to find a gameObject named "WeaponManagerScript". From your question it sounds like you're trying to find a component. so you'll need to use FindObjectOfType instead, and pass in the type (the script name, minus the .js).

http://docs.unity3d.com/Documentation/ScriptReference/Object.FindObjectOfType.html

Comment
Add comment · Show 14 · 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 Julien-Lynge · Jan 08, 2013 at 10:21 PM 1
Share

First you need a reference to the gameObject containing the script. Once you have that, you can access the component using GetComponent(). Alternatively, you can just look for the component directly (e.g. if there's just one component of that type anywhere in the scene). The latter is what I showed you to do.

Do you already have a reference to the gameObject containing the component you're looking for? If not, you'll need to use Find first to get the game object (pass the name of the game object you're looking for), and then GetComponent.

avatar image Julien-Lynge · Jan 08, 2013 at 10:44 PM 1
Share

Alright, now we're getting somewhere :)

First, I'm going to assume that there could be many Weapon$$anonymous$$anagers in the scene. If so, what you'd want to do is the following:

GameObject.Find("Weapon$$anonymous$$anager").GetComponent("Weapon$$anonymous$$anager");

(note that I'm a C# programmer, and this is from memory, so it might not work 100% out of the box)

That finds a game object called Weapon$$anonymous$$anager, and then gets a script on that game object called Weapon$$anonymous$$anager.

If you only have one weapon manager in scene, things get much easier:

FindObjectOfType(Weapon$$anonymous$$anager);

On that one, I'm not 100% sure of the syntax - perhaps somebody with more JS experience can correct it.

avatar image fafase · Jan 08, 2013 at 10:48 PM 2
Share

@julien

 GameObject.Find("Weapon$$anonymous$$anager").GetComponent("Weapon$$anonymous$$anager");

this could be problematic and will probably throw a warning. You need to cast or:

 GameObject.Find("Weapon$$anonymous$$anager").GetComponent(Weapon$$anonymous$$anager);

@bpears you can read this http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/

avatar image Julien-Lynge · Jan 08, 2013 at 10:51 PM 1
Share

Nice link - I hadn't seen that before. @bpears, hopefully that answers your questions. If so, be sure to mark the answer as accepted and give @fafase a thumbs up on the comment.

avatar image fafase · Jan 08, 2013 at 11:04 PM 1
Share

I would need to see the line and what is around. Also, just try to remove and rewrite the line. It is stupid but for some reasons sometimes the error goes away.

Show more comments
avatar image
0

Answer by Great Alexander · Jan 08, 2013 at 10:25 PM

Try to add tag to the objects and use

GameObject.FindObjectWithTag("the object which you want");

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Gameover function calling before game ends help 2 Answers

How to make the camera is not break trough object? 2 Answers

Player instantiates Backwords 0 Answers

A node in a childnode? 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