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 caucasianFries · Aug 06, 2013 at 07:04 PM · variablemultiple objects

Referencing variables in another script on another object

I am having trouble pulling values from another script and this script is on another object. The error I receive is "NullReferenceException: Object reference not set to an instance of an object"

The errors occur when I try to reference the variables in TileMapMouse (lines 30,31). Any help is appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class CharacterMovement : MonoBehaviour {
     
     
     //calling other script
     TileMapMouse _tilemapmouse;
     
     //initialize variables
     private bool _unitSelected;
     
     
     void Start () {
         _tilemapmouse = GetComponent<TileMapMouse>();
         _unitSelected=false;
     }
     
     
     void Update () {
         Ray ray = Camera.mainCamera.ScreenPointToRay( Input.mousePosition );
         RaycastHit hitInfo;
         
         //select if mouse=is_over & click
         if(collider.Raycast( ray, out hitInfo, Mathf.Infinity ) && Input.GetMouseButtonDown(0)) {
             _unitSelected=true;
         }
         
         //move if selected=true & rightclick & movementmode=true 
         if(_unitSelected && Input.GetMouseButtonDown(1) && _tilemapmouse.movementMode){
             transform.position = _tilemapmouse.worldpos;
         }
     }
 }
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 robertbu · Aug 06, 2013 at 07:10 PM

You need to find the object and do the get get component on it:

 _tilemapmouse = GameObject.Find("OtherObjectName").GetComponent<TileMapMouse>();

alternately you can do:

 public TileMapMouse _tilemapmouse;

Then you can drag the object that contains the component onto the _tilemapmouse variable in the inspector. In that case, you would skip the initialization in Start().

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 caucasianFries · Aug 06, 2013 at 07:42 PM 0
Share

Unfortunately there are many objects that will have this function. It is many planes (custom generation) that seamlessly sit together to create the view of a larger plane (This is due to the 65000 vertices limit within Unity).

Is there a simple way to get the name of the plane that I am hovering over in this script? Or, using your alternative, setting the 'public Tile$$anonymous$$ap$$anonymous$$ouse _tilemapmouse' to the object the curser is over?

avatar image robertbu · Aug 06, 2013 at 07:49 PM 1
Share

Actually yes. Insert between lines 25 and 26:

 _tilemapmouse = hit.collider.gameObject.GetComponent<Tile$$anonymous$$ap$$anonymous$$ouse>();

What you are doing is using the game object that the raycast hit. Note that if there is any chance at all that the raycast will hit something other than an object with a Tile$$anonymous$$ap$$anonymous$$ouse component, then this line will set _tilemapmouse to null. You other code would need to act appropriately.

avatar image caucasianFries · Aug 06, 2013 at 07:55 PM 0
Share

Thank you for your help!

EDIT: you must use a physics.raycast ins$$anonymous$$d of collider.raycast

avatar image
1

Answer by Ejlersen · Aug 06, 2013 at 07:11 PM

If its on another game object, then its because of this:

Line 15: GetComponent(), since it is not on the same game object.

If there is only one TileMapMouse script, then you could do one of several things, like:

  • Make TileMapMouse a singleton and use TileMapMouse.Instance to use it in other scripts.

  • GameObject.FindObjectOfType(typeof(TileMapMouse)) will search your hierarchy for a component of type TileMapMouse.

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 caucasianFries · Aug 06, 2013 at 07:38 PM 0
Share

Unfortunately there are many gameobjects which have this script.

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

15 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Select colour then change texture 0 Answers

Acces to a var from another Script. 2 Answers

Game Object referencing from another script 0 Answers

Find Object with variable, not a name. 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