- Home /
How to fix the platforms?
I have a problem with the platforms. I've made a trigger object and when it passes the ball that object,I made to create a new platform but two new platforms will be create and when I pass the second platform then four new platforms will be create.How to fix this, need to create one platform but create more?sorry bad english.
This is the code for new platforms
public Transform roadprefab;
void OnTriggerEnter(Collider other)
{
Instantiate(roadprefab, new Vector3(-301f,-3.1f,transform.parent.position.z + 500f),roadprefab.rotation);
}
}
Answer by zero_null · Jan 02, 2018 at 10:33 AM
The OnTriggerEnter will be called whenever the Object comes in contact with the trigger or colliders. You need to have a check, that'll see if the object is already created then you don't need to instantiate it again.
Transform obj;
public Transform roadprefab;
void OnTriggerEnter(Collider other)
{
if(obj!=null){
obj = Instantiate(roadprefab, new Vector3(-301f,-3.1f,transform.parent.position.z + 500f),roadprefab.rotation) as transform;
}
}
Answer by Kiko206 · Jan 02, 2018 at 11:33 AM
Assets/roadpref.cs(12,13): error CS0029: Cannot implicitly convert type UnityEngine.Transform' to
UnityEngine.GameObject' ??
Assets/roadpref.cs(12,108): error CS1061: Type UnityEngine.GameObject' does not contain a definition for
rotation' and no extension method rotation' of type
UnityEngine.GameObject' could be found. Are you missing an assembly reference? new problem
hahah looks like you never coded in Unity. The issue is because I just copied your code to show you how you can store the reference of the gameobject that'll help you in knowing if the object is created already.
Your answer
