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 Nova8808 · Aug 03, 2013 at 07:56 AM · raycastgetcomponent

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?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
 }
Comment
Add comment · Show 7 · 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 Bunny83 · Aug 03, 2013 at 08:29 AM 0
Share

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 :-)

avatar image Bunny83 · Aug 03, 2013 at 08:35 AM 0
Share

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

avatar image Nova8808 · Aug 03, 2013 at 08:44 AM 0
Share

Yes these scrips are attached to different objects. Can you not transfer a variable across scrips attached to different objects in unity?

avatar image Bunny83 · Aug 03, 2013 at 08:55 AM 0
Share

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

avatar image Nova8808 · Aug 03, 2013 at 09:17 AM 0
Share

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);
         }
         }
avatar image MichaelDShark Nova8808 · Nov 25, 2015 at 06:38 PM 0
Share

Sorry I'm only a beginner to C# But why is playerGO a Gameobject Thanks $$anonymous$$ichael

avatar image meat5000 ♦ MichaelDShark · Nov 25, 2015 at 07:10 PM 0
Share

Because its defined as one and in order to use GetComponent to reference the components on it.

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

17 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

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


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