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

1 Answer

1

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");
        }
    }
}

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