I am working on the platforming part of my game Maltrov. I have a script (below) that is supposed to duplicate a platform 5 times with each clone being in a new place. Whenever I attach the script to my platform and assign it as the parent as well as the Gameobject variables the project crashes. Is there a way to make the code work?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Duplicateplatform : MonoBehaviour
{
public GameObject newGameObject;
public Transform parent;
Vector3 newPosition;
public Quaternion newRotation;
public float xValue = 20f;
public float yValue = 20f;
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < 5; i )
{
xValue = 20f;
yValue = 20f;
duplicateObject(xValue, yValue);
}
}
public void duplicateObject(float xValue,float yValue)
{
// create (duplicate, in a new position, at a new rotation to the parent)
newPosition = new Vector3(xValue, yValue, 0);
Instantiate(newGameObject,newPosition,newRotation,parent);
}
}
I tried attaching the platform to an empty Gameobject, and then the script for the platform to the empty GameObject, but the project still crashed.
CodePudding user response:
I tried your code and it works fine in mine.
Therefore the problem lies elsewhere.
How about checking your inspector?
If you're getting null errors, check if you plugged in your newGameObject and Parent.
If your editor froze, it's possibly because you added the game object as your instantiate object, and it's in an infinite loop.