Initialize Prefab with all it's scripts
So I have two parts, one is GameController, which controls the creation of my prefabs, let's say whenever, and I have a prefab tile which is meant to move down on the screen. So far, I have this code for my Tile:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(new Vector2(0, -1) * ControllerScript.speed * Time.deltaTime);
}
}
And here is the GameController: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ControllerScript : MonoBehaviour {
public static float speed;
// Use this for initialization
void Start () {
ControllerScript.speed = 5f;
}
// Update is called once per frame
void Update () {
TileGenerator();
}
private void TileGenerator(){
GameObject tile = GameObject.Instantiate(Resources.Load("Tile_Yellow")) as GameObject;
tile.transform.position = new Vector3(0f, 4.5f, 3f);
}
}
However, it doesn't generate my prefab at all. Anyone can modify the code? I'll keep looking around myself as well.
Thank you :)
Answer by TeohRIK · Apr 23, 2017 at 01:40 AM
I think something wrong with your Resources.Load function
Maybe you can try like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControllerScript : MonoBehaviour {
public static float speed;
// Use this for initialization
void Start () {
ControllerScript.speed = 5f;
}
// Update is called once per frame
void Update () {
TileGenerator();
}
private void TileGenerator(){
GameObject targetTile = Resources.Load("Tile_Yellow") as GameObject;
GameObject tile = GameObject.Instantiate(targetTile ) as GameObject;
tile.transform.position = new Vector3(0f, 4.5f, 3f);
}
}
Or the other way is you can assign a public GameObject and drag in your tile prefab from the inspector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControllerScript : MonoBehaviour {
public static float speed;
public GameObject yellowTilePrefab;
// Use this for initialization
void Start () {
ControllerScript.speed = 5f;
}
// Update is called once per frame
void Update () {
TileGenerator();
}
private void TileGenerator(){
GameObject tile = GameObject.Instantiate(yellowTilePrefab) as GameObject;
tile.transform.position = new Vector3(0f, 4.5f, 3f);
}
}
Hi so yeah I tried your way my friend, but now I have a problem. the "Tile_Yellow" doesn't get created at all. When I call my method. So here is a picture when I run it:
![alt text][1]
But I want it to create it and drop it on the screen much like this: ![alt text][2]
So Im not sure where I am wrong exactly. Cause I created the prefab gameObject, expecting it to follow all the scripts I have given to it. [1]: /storage/temp/92657-screenshot-from-2017-04-22-21-49-36.png [2]: /storage/temp/92658-screenshot-from-2017-04-22-21-52-31.png
you using the first method or the second method?
From what I understanding, in order to load the resources by using the Resources.Load function you need to put the asset or the prefab under the folder name called Resources.
I can see you put your prefab under the wrong folder from the picture you provided. Try to create a new folder call Resources and drag your prefab to that folder
So weird, the Resources.Load method does work on my side. Can I know whats the error u get?
Answer by BaldBeardedMonk · Apr 23, 2017 at 03:41 AM
Do not use Resources.Load . its slow because it has to load stuffs from the disk. and hence can cause a bit of lag in mobile devices. What you can do is create a public GameObject variable. and assign the tile prefab in the inspector. Then you can instantiate it using this code.
public GameObject Tiles;
Then assign the prefab in the inspector to this Tiles. Then use the below code to instantiate
GameObject G = (GameObject)Instantiate(Tiles,tile.transform.position,Quaternion.identity);
Also make sure to put your prefab in the Prefabs (or assets) folder and not in the Resources folder. Infact please remove anything from the Resources folder that is not needed as it increases the size of your build.