- Home /
How to make a game object from several game objects in Unity?
Dear Unity fourm I have programmed a great music game. Unfortunately there is a problem. In my game there are so many objects that my smartphone can't load the game properly. I have nowhere to find out how to make an object out of many objects. If someone knows how to do it I would be very happy!
My Script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class line : MonoBehaviour {
//Please attach me to the line, then set dline,tail,mcamera,road,decorates,dieeff.
//You can add more to make this better!
public GameObject dline, tail, mcamera, road, decorates, dieeff;
private GameObject tempgo, tempdia, tempcr;
public bool direction, alive, start, load, roadmaker;
public float cameraspeed, temprm;
public Vector3 offset, tempcrgo;
private GameObject[] dia, cr;
List<GameObject> go = new List<GameObject>();
void Start()
{
start = false;
load = true;
alive = true;
//find diamonds and crowns
dia = GameObject.FindGameObjectsWithTag("dia");
cr = GameObject.FindGameObjectsWithTag("crown");
//
}
void Update()
{
if (load == true)
{
//save the initial distance between camera and line
offset = mcamera.transform.position - dline.transform.position;
cameraspeed = 0.03f;
load = false;
//
}
//restart
if (Input.GetKeyDown(KeyCode.R) == true)
{
SceneManager.LoadScene("main");
}
//
if (start == false && alive == true)
{//start game
if (Input.GetMouseButtonDown(0) == true || Input.GetKeyDown(KeyCode.Space) == true)
{
start = true;
dline.GetComponent<AudioSource>().enabled = true;
}
}//
if (start == true)
{//turn
if (Input.GetMouseButtonDown(0) == true || Input.GetKeyDown(KeyCode.Space) == true)
{
if (direction == true)
{
direction = false;
}
else
{
direction = true;
}
}
}//
//build road/wall more efficiently
if (roadmaker)
{
temprm += 1;
//why there is temprm?to reduce the ammount of roads
//why build one when click?to ensure there is road/wall when line turns(or it will look weird)
if (temprm % 6 == 1 || Input.GetMouseButtonDown(0) == true || Input.GetKeyDown(KeyCode.Space) == true)
{
GameObject.Instantiate(road, dline.transform.position + new Vector3(3, 0, -3), dline.transform.rotation);
GameObject.Instantiate(road, dline.transform.position + new Vector3(-3, 0, 3), dline.transform.rotation);
}// // //
}
else
{//when you play ...I dont put the part in Update but if(roadmaker) because these actions will create new gameobject and interfere the copying of road
GameObject.Instantiate(tail, dline.transform.position, dline.transform.rotation);
if (Input.GetMouseButtonDown(0) == true || Input.GetKeyDown(KeyCode.Space) == true)
{
if (direction == true)
{
go.Add(GameObject.Instantiate(decorates, dline.transform.position + new Vector3(8, -9, -5), dline.transform.rotation));
}
else
{
go.Add(GameObject.Instantiate(decorates, dline.transform.position + new Vector3(-5, -9, 8), dline.transform.rotation));
}
}
}//
}
void FixedUpdate()
{//smooth camera
mcamera.transform.position = Vector3.Lerp(mcamera.transform.position, offset + dline.transform.position, cameraspeed);
//
//some 'animations' .I didnt use animation component because I dont know how it works in Unity :(
foreach (GameObject tempgo in go)
{
tempgo.transform.position += new Vector3(0, 0.2f, 0);
}
foreach (GameObject tempdia in dia)
{
tempdia.transform.localEulerAngles += new Vector3(0, 2, 0);
if (Mathf.Abs(dline.transform.position.x - tempdia.transform.position.x) < 1 && Mathf.Abs(dline.transform.position.z - tempdia.transform.position.z) < 1 && tempdia.transform.localScale.z > 0)
{
tempdia.transform.localScale -= new Vector3(0.3f, 0.3f, 0.3f);
}
}
foreach (GameObject tempcr in cr)
{
tempcr.transform.localEulerAngles += new Vector3(0, 2, 0);
if (Mathf.Abs(dline.transform.position.x - tempcr.transform.position.x) < 2 && Mathf.Abs(dline.transform.position.z - tempcr.transform.position.z) < 2 && tempcr.transform.localScale.z > 0)
{
tempcr.transform.localScale -= new Vector3(0.26f, 0.26f, 0.26f);
tempcrgo = tempcr.transform.position;
}
if (tempcr.transform.localScale.z <= 0)
{
tempcrgo += new Vector3(Random.Range(-2f, 2f), Random.Range(-1f, 2f), Random.Range(-2f, 2f));
tempcr.GetComponent<Light>().enabled = true;
tempcr.transform.position = Vector3.Lerp(tempcr.transform.position, tempcrgo, 0.02f);
}
}
//
if (start == true && alive == true)
{//how the line move
if (direction == true)
{
dline.transform.position += new Vector3(0.3f, 0, 0);
}
else
{
dline.transform.position += new Vector3(0, 0, 0.3f);
}
}//
}
void OnCollisionEnter(Collision x)
{//when die...
if (x.collider.tag == "wall")
{
alive = false;
mcamera.GetComponent<AudioSource>().enabled = true;
dieeff.SetActive(true);
}//
//when complete the level...
if (x.collider.tag == "Finish")
{
offset = offset + offset + offset + offset;
cameraspeed = 0.01f;
}//
}
}
`
Video Link:
https://drive.google.com/file/d/1dqIqu6tx5thNbCJ1jNndXEJY_a9JJGGe/view?usp=sharing
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
How to keep a GameObject inside a collider? 3 Answers
gameobject.transform.parent not assinged after deploy 0 Answers
Unwanted Cube GameOjbect gets generated upon playing any scene? 1 Answer
Must I always create a public variable for Prefabs? 2 Answers