Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by mehrzadb · Apr 23, 2017 at 01:18 AM · c#movementprefabcreate

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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);
       }
   }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mehrzadb · Apr 23, 2017 at 01:55 AM 0
Share

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

screenshot-from-2017-04-22-21-49-36.png (213.0 kB)
screenshot-from-2017-04-22-21-52-31.png (248.2 kB)
avatar image TeohRIK mehrzadb · Apr 23, 2017 at 02:11 AM 0
Share

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

avatar image mehrzadb TeohRIK · Apr 23, 2017 at 03:32 AM 0
Share

Okay let me try fixing that.

Show more comments
avatar image TeohRIK mehrzadb · Apr 23, 2017 at 04:44 AM 0
Share

So weird, the Resources.Load method does work on my side. Can I know whats the error u get?

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

351 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My Prefab goes crazy and it moves faster when i click in another thing of the unity editor or the scene even if that didn't suppose to happen 0 Answers

Player loses momentum when landing 0 Answers

Question on rigidbody character rotating to the degree of input and then moving 0 Answers

I have problems with scripting movement, anyone can help me? 0 Answers

How to push player left and right 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges