- Home /
Pulling raycast hit point from one script into another gives error
In a script SpellCast I get a ray hit point when the mouse is clicked:
public class SpellCast : MonoBehaviour
{
private Vector3 targetPoint;
float hitdist;
void Start ()
{
}
void Update ()
{
if (Input.GetMouseButtonDown(1))
{
Plane playerPlane = new Plane(Vector3.up, PlayerTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist))
{
targetPoint = ray.GetPoint(hitdist);
}
}
}
public Vector3 hit_out()
{
return targetPoint;
}
}
Then in another script SpellAim i'm simply trying to pull in the hit point vector3:
using UnityEngine;
using System.Collections;
public class SpellAim : MonoBehaviour
{
private SpellCast spellc;
Vector3 rayhitout;
void Start ()
{
spellc = GetComponent<SpellCast>();
}
void Update ()
{
rayhitout = spellc.hit_out();
Debug.Log(rayhitout);
}
}
The game runs but I get the error Null Reference Exception- Object reference not set to an instance of an object. Any ideas?
Answer by fafase · Aug 03, 2013 at 08:01 AM
You would go better probably going the other way around. In SpellCast, add a referenc to the SpellAim script. Then add a method in SpellAim that is called from SpellCast when you do the raycast. Thus method takes a vector3 or raycast hit as parameter.
SpellCast.cs
SpellAim sa;
void Start(){
sa = GetComponent<SpellAim>();
}
void Update(){
// Your raycast thing
// If raycast
sa.GetHitInfo(hit);
}
SpellAim.cs
public void GetHitInfo(RaycastHit hit){
// do your stuff with hit
}
I don't see a problem with his approach. The only thing might be that those two scripts are not on the same GameObject and that's why the get component is failing.
BTW he uses a Plane object for the Raycast, so there is no RaycastHit :-)
PS. What might Ber a problem is that he might got a one frame delay with his approach. Your way has a problem when you want to serve multiple scripts that the Raycast script have to reference all receiver objects. It might be easier with Send$$anonymous$$essage
Yes these scrips are attached to different objects. Can you not transfer a variable across scrips attached to different objects in unity?
Of course you can, but if you just use GetComponent you call the GetComponent method of the GameObject the calling script is attached to. This will only give you SpellCast scripts attached to the same GameObject.
If you make your spellc variable public you can drag and drop the other GameObject / script instance on the variable. That way you don't need GetComponent at all.
Just take a look at this:
https://docs.unity3d.com/Documentation/$$anonymous$$anual/ControllingGameObjectsComponents.html
Thanks Bunny! Creating a gameobject and dragging the gameobject with SpellCast on it over worked.
public class SpellAim : $$anonymous$$onoBehaviour
{
private SpellCast spellc;
Vector3 rayhitout;
private float speed = 2.0f;
public GameObject playerGO;
void Start ()
{
spellc = playerGO.GetComponent<SpellCast>();
}
void Update ()
{
rayhitout = spellc.hit_out();
Debug.Log(rayhitout);
}
}
Sorry I'm only a beginner to C# But why is playerGO a Gameobject Thanks $$anonymous$$ichael
Because its defined as one and in order to use GetComponent to reference the components on it.
Your answer
Follow this Question
Related Questions
GetComponent, Object Reference not set to an instance of an object 0 Answers
how to make one script change a variable in another scipt 2 Answers
How can I detect for 2 different component types using an interface on RayCast Hit? 1 Answer
How can I pre-load all the possible components to avoid stuttering with GetComponent? 0 Answers
How do I get my Fireball toward my mouseposition?(some non working tries example in here :3) 1 Answer