• 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 Uni · Apr 07, 2011 at 07:09 AM · targetonmousedown

Setting GameObject as target in another script

Hi

I have two scripts, PlayerController.cs and Target.cs in PlayerController there is a public GameObject target;

In Target.cs I want to set the gameobject I click as target in the PlayerController script, but obviously I messed up somewhere.. anyone have a hint?

The first line works just fine as far as I can see. Debug.Log prints out the name of the object I click. I am also able to access target and other int's etc from the PlayerController using pc. so I guess the third line works? However I cant set the target of PlayerController to "go"

Error: NullReferenceException: Object reference not set to an instance of an object

Target.cs

    void OnMouseDown() {
    GameObject go = this.gameObject;
    Debug.Log(go);
    PlayerController pc = (PlayerController)GetComponent("PlayerController");
    pc.target = go;
}

Anyway, thanks for looking

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 KeithK · Apr 07, 2011 at 07:22 AM 1
Share

Is your PlayerController script an attached component of the clicked on Target? If not, you will have null returned. The syntax should also be GetComponent();

avatar image Uni · Apr 07, 2011 at 07:43 AM 0
Share

Ofcourse.. I have the PlayerController on the player and the Target on the target. no wonder it wont work Guess I have to rewrite the Target script

2 Replies

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

Answer by KeithK · Apr 07, 2011 at 07:48 AM

Ah yes, you were on the right track though. Just missing the step of getting a reference to your player, to get it's script component.

void OnMouseDown() { GameObject.Find("ThePlayerObjectsName").GetComponent<PlayerController>().target = gameObject;

// Or if you have the Player setup with a Tag

GameObject.FindWithTag("TagName").GetComponent<PlayerController>().target = gameObject; }

=)

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 Uni · Apr 07, 2011 at 08:21 AM 0
Share

Thank you I got it working now.. now I just need to make "the rest" and I have a completed game ;) should be easy

avatar image
0

Answer by paulus51 · Jan 06, 2013 at 05:47 PM

coulfd you place the whole scripts please ? that wouyd be greatfull

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 AlucardJay · Jan 06, 2013 at 05:47 PM 0
Share

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.

You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]

Under the answer where it says edit | delete | more , click on more , then convert to comment

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

2 People are following this question.

avatar image avatar image

Related Questions

iOS game: Is it possible to target iphone 4 and ipad only? 1 Answer

Track objects in camera view using GUI? Like a HUD for flight sim lockon targeting. 1 Answer

Follow Nearest Target 1 Answer

Targetting Script Help 1 Answer

Mouse lock to target 0 Answers


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