How to make 2 platforms act individually with same script

Hey,

I want to make a falling platform that does all the dirty work for me.

So what i did before:

I created a platform(simple cube), on that platform i put a trigger.
Then i had to scripts, one on the platform itself to move it when the trigger was hit, one on the trigger with a bool for when its hit, to use in the platform script.

Now this is something that works fine until you want 10 or even 100 platforms

Aside from all the dirty work there is a problem. when i make 2 platforms with 2 triggers, using the same triggerscript(with the bool) Both platforms will fall down when i stand on one of them, cause they use the same script so the same bool. Thats not what i want. So apparently 2 objects using the same script cant act individually!

But you dont wanna make 100 scripts for 100 triggers, all doing the same thing! So how can you solve this problem?

Then to my goal, for solving this problem i started working on a single script to put on a platform that makes his own trigger (via code), so that every platform has its own trigger (and you dont have to do it yourself manually). But now i want to create a script in script to put on the trigger (made in script). So 1 script on a platform creates his own trigger and triggerscript to handle the action.

So every platform acts individually and can simply be copied.

Can this work? or is there a better way to avoid making seperate scripts for the same job on different platforms for them to act individually?

Thanks.

This is what i have so far:

var Gravity: float;
var time: float;
var FallingSound: AudioClip;
var SoundPlayed: boolean;
var OriginalPos: Vector3;
var OriginalPosTarget: Vector3;
var OriginalPosTrigger: Vector3;

var target : Transform;
private var controller : FallingPlatformTriggerScript;

private var Trigger: GameObject;

function Awake()
{
OriginalPos = transform.position;
OriginalPosTarget = target.position;
OriginalPosTrigger = transform.position;
OriginalPosTrigger.y += 1;

Trigger = GameObject.CreatePrimitive(PrimitiveType.Cube);
Trigger.renderer.enabled = true;
Trigger.collider.isTrigger = true;
Trigger.transform.position = OriginalPosTrigger;
Trigger.collider.bounds.size = transform.collider.bounds.size;
}

function Update()
{
if (target)
{
controller = target.GetComponent(FallingPlatformTriggerScript);
}
else{return;}

if(controller.IsHit == true)
{
time += Time.deltaTime;

if(!SoundPlayed)
{
audio.PlayOneShot(FallingSound);
SoundPlayed = true;
}

var Total = (0.5Gravity(time * time));
transform.Translate(Vector3.down * Total);
target.Translate(Vector3.down * Total);
Trigger.transform.Translate(Vector3.down * Total);
}

if(transform.position.y < -50)
{
Reset();
}
}

function Reset()
{
controller.IsHit = false;
time = 0;
transform.position = OriginalPos;
target.position = OriginalPosTarget;
Trigger.transform.position = OriginalPosTrigger;
SoundPlayed = false;
}

Every instance of a script will act independent of the others, unless you have specifically marked a variable or function as static. What leads you to believe otherwise?

Create a platform with a trigger and a script registering the trigger, etc. Now just store this gameObject as a prefab and drop as many instances (that act independently) in your scene as you want.

Hmm, never thought of it that way. Thanks, i will try that.
Although when you just make 2 seperate platforms with the same script for hitdetection (ontriggerenter) without using prefabs, they don’t act independently. Even without static variables. I’ve tried it. So i guess for some reason, although the script is used twice, the 2 platforms work on the same script and variables instead of making copies.

Anyway thanks for the fast reply!

Hey,

I have a second question regarding this problem.
What you said earlier worked for the falling platform. (so thanks for that)
But now im working on a platform to stand on that you can control.
So if you stand on it, it will go up, if you are on de left side of the platform it will move to the left, on the right side it will move to the right.
So i have my platform with on top of that 3 triggers, one in the middle to move up, and 2 on each end of the platform to move sideways.

For now the platform and triggers are all independent, so no parent/child and in the platform script i move the triggers along with the platform. Works perfectly.
But if i want to make it a prefab, I will have to use the parent/child option to make it a one, and disable the movement in the script, because childs move automatically with their parents. But there’s the problem. For some reason the childs move a little bit slower than the parent, wich will get my triggers end up under the platform in a few seconds, so you cant control the platform anymore.

What can I do to make the childs move at the same speed as the parent?

Thanks in advance.

Nope.

I don’t understand what you are doing in the given script, but scripts created separately act independently of each other. If they both react in the same way to a event, that is because somewhere you’ve told both of them to act to the event.

Exactly, as NPSF3000 said. And if no manipulation of the translation of a child object is caused by scripts, the child will move exactly as fast a the parent.

Hmm weird, I’ll check again.

Hmm solved it.

When I made my platform it had a rigidbody on it, and to make my trigger I simply copied the platform and made it a trigger.
It seems if the trigger has a rigidbody, it wont follow its parent correctly.
I’ve deleted the rigibody and now it works fine.

Thanks for the help, Appriciate it!