Multiple objects

Hi all,

I have many objects running the same script. This 1 script also deals with all collisions.
For reasons I can’t fathom; sometimes when a collision occurs the variables for that game object are all null; even though they were set in start().
I assume this is some c# variable overriding/shadow thingy :slight_smile:
My lack of knowledge of c# is the issue here I suspect.
But why would they be null in any circumstance when set initially? (some kind of scope issue???)

Cheers

Edit:
For offending lines are:

collision_table = table3;
script_bomb = GetComponent();

These are definitely set. But in an Ontriggerenter although game object.name is perfect these guys are null!!!.

are the “objects” prefabs?

explain what you’re doing a little further…

Hi,

Yes they are prefabs.
Well (for example)

Start does:

go = GameObject.Find(“character1”);
player = go.GetComponent();

Which is always correct.

But in my ontriggerenter sometimes player is null!!
I assume player is unique for just this object and therefore will always be correct as initialised in start().
This is also true for some other local variables. I looked at public/protected/private settings; and again assume public should be fine.
I guess I’m not understanding/mis-using c# somehow.

Cheers

hhmm, and this script is attached to every object??

Yep :slight_smile:
I thought it would be clever (sigh) to have 1 script deal with all collisions; rather than an ontriggerenter attached script per object.
It seemed a neater solution as the outcome of all collisions are in 1 place. Though I am beginning to wonder if it’s worth the hassle. :slight_smile:

Cheers

i’m not understanding what you’re doing though. write a description what’s actually going on.

Actually. I’ve just had a thought.

I believed that inside an ontriggerenter the context of the local variables were maintained. Because the ontriggerenter is in the same script I assumed the variables held their value. I am guessing they do not. I guess this because technically the ontriggerenter could be anywhere and therefore will not know the local scope and context.

Is that correct do you think?

Cheers

i dont know because I’m clueless to what you’re trying to do how everything is set up…

OMG! that seems to be correct!

So setting anything in start() is potentially lost in the ontriggerenter
HUFF!, that means all my init stuff is pants.

i.e. before I did in start()

if (gameObject.name == “Bomb”)
{
collision_table = table3;
script_bomb = GetComponent();
}

Already for a collision that might occur.

Inside ontriggerenter collision_table and script_bomb were null!!!

So I have to do the above actually within in the ontriggenter - bit of a bummer really; because as a generic collision function it now has to find/findtag the player and indeed anything else it may need rather than have them pre saved for speed.

Of course I could have this all wrong :-))))

Cheers

Hmmmm!
It would seem that the base class variables can be seen but not accessed i.e. it’s all null.
Anyone with a far better understanding of c# please explain this?

Cheers