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
1
Question by FarKing · Apr 14, 2013 at 04:47 AM · c#raycastgetcomponentphysics.raycast

using Physics.Raycast to run functions on diffrent gameObjects?

Hi im having trouble using Physics.Raycast to run functions on a game object it seems somewhat simple but i cant for the life of me see what im missing. If anyone could look over this bit of code for me and give me a hint for what im missing i would be very much in there debt

GUIScript

 public bool _Scanning = false;
 public Ray ray;
 public RaycastHit hit;
 // Update is called once per frame
 void Update () {
     if (_Scanning == true){
         if(Input.GetButton("Fire1")) {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             Scan();
         }
     }
 }
 private string type;
 private int surfTemp;
 private float mass;
 private float radius;
 
 void Scan(){
     if (Physics.Raycast(ray, out hit, 20)){
     Debug.DrawLine(ray.origin, hit.point);
         if (hit.transform.gameObject.GetComponent<StarTypeA>()){
             type = StarTypeA.GetStarType();
             surfTemp = StarTypeA.GetStarTemp();
             mass = StarTypeA.GetStarMass();
             radius = StarTypeA.GetStarRadius();
             _Scanning = false;
             Menu ("showScan");
         }
     }
 }

Stars Script (that StarTypeA inherits from)

 string type;
 
 int surfTemp;
 float mass;
 float radius;
     
 string desc;
 
 public int planetCount;
     
 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     
 }
 public string GetStarType (){
     return type;
 }
 public int GetStarTemp(){
     return surfTemp;
 }
 public float GetStarMass(){
     return mass;
 }
 public float GetStarRadius(){
     return radius;
 }
 public int GetPlanetCount(){
     return planetCount;
 }

At the moment the error im getting is: 'error CS0120: An object reference is required to access non-static member'

But i was under the impression that i had an object reference from the raycast but now i just dont know. so im guessing that i have missed something somewhere.

again any help would be very much appreciated

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
1
Best Answer

Answer by Chronos-L · Apr 14, 2013 at 04:52 AM

You get a 'error CS0120: An object reference is required to access non-static member' because your GetStartType() are non static function that cannot be accessed using this line 'StarTypeA.GetStarType()`, the same goes for other functions:

 void Scan(){
     if (Physics.Raycast(ray, out hit, 20)){
     Debug.DrawLine(ray.origin, hit.point);
         if (hit.transform.gameObject.GetComponent<StarTypeA>()){
             //Calling non-static function in a static way
             type = StarTypeA.GetStarType();
             ...
         }
     }
 }



But i was under the impression that i had an object reference from the raycast

You have the GetComponent<>(), and it returns a component/null, but you never assign it to anything at all, so the returned item is used once in the if-statement.

Correction

 void Scan(){
     if (Physics.Raycast(ray, out hit, 20)){
         StarTypeA sta = hit.transform.gameObject.GetComponent<StarTypeA>();  
  
         if ( sta != null ){
             type = sta.GetStarType();
             surfTemp = sta.GetStarTemp();
             mass = sta.GetStarMass();
             radius = sta.GetStarRadius();
             _Scanning = false;
             Menu ("showScan");
         }
     }
 }
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 FarKing · Apr 14, 2013 at 02:49 PM 0
Share

many many thanks Chronos-L i was starting to feel like i was just running in to a wall. i cant grant karma yet but when i can i will

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

12 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

Related Questions

how to make one script change a variable in another scipt 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Bottom area of a 3D model? Raycast? 1 Answer

How to get the distance between two objects in feet/meter? 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