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 /
This question was closed Apr 07, 2014 at 11:19 PM by Benproductions1 for the following reason:

Duplicate Question

avatar image
0
Question by Aburkins · Apr 07, 2014 at 11:00 PM · nullreferenceexceptionaccessing scriptsmouse input

How to access other scripts from one script

I am trying to write code to move other GameObjects in a scene to a new location when a specific GameObject is clicked with the mouse. Right now I have the following test code:

     void Update () {
         if(Input.GetMouseButtonDown(0)){
             Move m = GetComponent<Move>();
             m.doSomething();
         }

Move is a separate script that contains the method:

 public void doSomething () {
 
         transform.position = new Vector3(0, 0, 0);
         }

When I run this, I get the "NullReferenceException: Object reference not set to an instance of an object" error. Any ideas why?

Edit: I forgot to mention that Move is attached to a different object. When I click on an object with the first script attached, I want the object with the Move script to move.

Comment
Add comment · Show 7
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 xt-xylophone · Apr 07, 2014 at 11:05 PM 0
Share

Is your script called $$anonymous$$ove attached to the same object?

avatar image Aburkins · Apr 07, 2014 at 11:06 PM 0
Share

No, it is attached to a different object.

avatar image xt-xylophone · Apr 07, 2014 at 11:07 PM 0
Share

Theres your issue. calling GetComponent on its own will get a component from the object that script is attached to. You will need to be like 'otherObject.getComponent()' to get the $$anonymous$$ove script from the other object :)

avatar image Aburkins · Apr 07, 2014 at 11:12 PM 0
Share

Ah, I see. But how do I reference the other object? I can't say Cube2.getComponent(), can I?

avatar image xt-xylophone · Apr 07, 2014 at 11:17 PM 0
Share

There are lots of ways to get that other object. You could declare a public variable at the start of your script like

 public Transform otherObject;

Drag the other object into it in the editor and then call

 $$anonymous$$ove m = otherObject.getComponent<$$anonymous$$ove>();

This only works if you can point to that object specifically though.

You could various find like GameObject.Find or FindObjectsWithTag to get it. These are a lot more costly operations than the first, so use these during the Start method so its only once and save that reference to $$anonymous$$ove m.

Also, you can have lots of scripts on any object, maybe you could attach your script to the same object as the one $$anonymous$$ove is on?

Theses are just some examples. Id need more context if you want some more specific advice, hopefully you can take it from here though :D

Show more comments

0 Replies

  • Sort: 

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

Access a counter in another script in scene(NullReferenceException: Object reference not set to an instance of an object) 1 Answer

troubleshooting NullReferenceException 2 Answers

NullReferenceException 2 Answers

Material[] Object reference not set when instantiating 2 Answers

NullReference Exception when adding a class 2 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