- Home /
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;
}
}
}
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().
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?
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.
Thank you for your help!
EDIT: you must use a physics.raycast ins$$anonymous$$d of collider.raycast
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.
Unfortunately there are many gameobjects which have this script.