x


Accessing non-static member

Hello, I'm using the Orthello2d asset. Using C#, I've set up a delegate to handle collisions and am trying to print the name of the object colliding with the player, however receive the error:

An object reference is required to access non-static member `UnityEngine.Collision.gameObject'

public class Player : MonoBehaviour 
{
 void Start () 
    {
        OTObject player;
        player = GetComponent<OTSprite>();
        player.onCollision = OnCollision;
 }

    public void OnCollision(OTObject owner)
    {
        print("Player collision");
        string objectName = Collision.gameObject.name;  //Game runs fine
        print(objectName);                              //with these lines removed
    }
}

I am not very experienced, so my guess is somewhere along the lines of "I need to make an instance or something of the Collision member first or 'declare' something..." I don't know.

more ▼

asked Jul 17 '12 at 06:57 PM

Ishkur gravatar image

Ishkur
69 5 9 11

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Apart from learning, do you have a good reason for not using the collision detection of the engine? It's doing it all for you. But if you want to learn then I understand.

Now your problem might be that you declare a OTObject sprite but the GetComponent returns a OTSprite. So it probably returns null.

*OTObject* sprite;
sprite = GetComponent<*OTSprite*>();
more ▼

answered Jul 17 '12 at 07:03 PM

fafase gravatar image

fafase
11.1k 11 15 45

The Orthello framework has tools for collisions between its object, but according to the user manual, it uses Unity's collider system. So I'm pretty sure this is the simplest way to handle the collisions.

If I remove the line that tries to receive the colliding object's name, the collision is detected just fine. The problem is trying to find the name of the collider.

Jul 17 '12 at 07:11 PM Ishkur

Try with this:

public void OnCollision(OTObject owner)
{
    print("Player collision");
    string objectName = owner.gameObject.name;  
    print(objectName);                       
}

Notice that you pass a OTObject owner but use Collision. What is this? (I am not familiar with the Orthello framework so my guess could be wrong). Still, you should consider what I mentioned in the answer since you tried to pass a OTSprite to a OTObject (then again Orthello might allow that).

Jul 17 '12 at 07:19 PM fafase

Hm... that just returns the name of the player, not the object colliding with it.

That's a funny what you pointed out, though. I hadn't noticed that I had declared an OTObject but then passed OTSprite from then on. It works in Orthello, but I guess it is developing a bad habit in me.

Jul 17 '12 at 07:37 PM Ishkur

I'm sorry for wasting your time. What I had needed was

owner.collisionObject.name;

with collisionObject being a method of Orthello's OTObject, so nobody would have been able to answer the question without knowledge of the package.

I would still like to know how I would get the name of the colliding GameObject using Unity's script, however, as I'm pretty convinced my original code should have worked without that non-static member error.

Jul 17 '12 at 07:42 PM Ishkur

Well, I did tell you to use:

string objectName = owner.gameObject.name;

I was close!! Now in UnityScript, it goes:

function OnCollisionEnter(other:Collision){
    if(other.gameObject.tag=="Anything")
        //Do something }

You pass the other object, this one is colliding with. You can access all types of variables and functions with the reference other. In my exemple, I check the tag of the object to be anything and perform an action if it is, colliding with an object tagged "Something" will not do.

Jul 17 '12 at 08:29 PM fafase
(comments are locked)
10|3000 characters needed characters left

To get the Colliding object use OTObject owner.collisionObject To get the Collision use OTObject owner.collision

more ▼

answered Oct 18 '12 at 04:50 PM

mas gravatar image

mas
36

(comments are locked)
10|3000 characters needed characters left

If you want to access a non-static variable or function, you need an object with the respective script in the scene. Having the script in the Project Folder alone is not enough. Only static functions/variables can be accessed without having them in the scene.

more ▼

answered Jul 17 '12 at 07:00 PM

Piflik gravatar image

Piflik
5.4k 17 27 45

Yes, this script is in the scene and attached to the player whose collisions are being checked against other objects.

Jul 17 '12 at 07:11 PM Ishkur
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4377
x2584

asked: Jul 17 '12 at 06:57 PM

Seen: 2203 times

Last Updated: Oct 18 '12 at 04:50 PM