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 /
avatar image
0
Question by joshocaoimh · May 02, 2020 at 12:57 AM · prefabtransform.positiongridspawningprefab-instance

How to instantiate prefabs with scripts attached?

I've a grid that spawns two different colour cubes onto it. Now I am trying to swap out the cubes for prefabs which I've attached scripts to but when the prefabs spawn the scripts are gone. I think I might be supposed to use Gameobjects for the prefabs but I'm not really sure what I'm doing.

EDIT: I know I can set the pink and yellow Cube to prefabs that are already in the scene and the scripts will work but I'd like to be able to instantiate prefabs that arent in the scene.

Any help would be much appreciated!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MapGenerator : MonoBehaviour {
     
     public Transform tilePrefab;
     public Transform pinkCube;
     public Transform yellowCube;
     public Vector2 mapSize;
     public Vector2 zeroZero;
     
     [Range(0,1)]
     public float outlinePercent;
 
     List<Coord> allTileCoords;
     Queue<Coord> shuffledTileCoords;
 
     public int seed = 10;
     
     void Start() {
         GenerateMap ();
     }
     
     public void GenerateMap() {
         
         zeroZero = mapSize/2;
 
         allTileCoords = new List<Coord> ();
         for (int x = 0; x < mapSize.x; x ++) {
             for (int y = 0; y < mapSize.y; y ++) {
                 allTileCoords.Add(new Coord(x,y));
             }
         }
         shuffledTileCoords = new Queue<Coord> (Utility.ShuffleArray (allTileCoords.ToArray (), seed));
 
         string holderName = "Generated Map";
         if (transform.Find (holderName)) {
             DestroyImmediate (transform.Find (holderName).gameObject);
         }
         
         Transform mapHolder = new GameObject (holderName).transform;
         mapHolder.parent = transform;
         
         for (int x = 0; x < mapSize.x; x ++) {
             for (int y = 0; y < mapSize.y; y ++) {
                 Vector3 tilePosition = CoordToPosition(x,y);
                 Transform newTile = Instantiate (tilePrefab, tilePosition, Quaternion.Euler (Vector3.right * 90)) as Transform;
                 newTile.localScale = Vector3.one * (1 - outlinePercent);
                 newTile.parent = mapHolder;
             }
         }
 
         //Pinks
         int pinkCount = 1;
         for (int i =0; i < pinkCount; i ++) {
             Coord randomCoord = GetRandomCoord();
             Vector3 obstaclePosition = CoordToPosition(randomCoord.x,randomCoord.y);
             Transform newObstacle = Instantiate(pinkCube, obstaclePosition + Vector3.up * .5f, Quaternion.identity) as Transform;
             newObstacle.parent = mapHolder;
         }
         
         //Yellows
         int yellowCount = 5;
         for (int i =0; i < yellowCount; i ++) {
             Coord randomCoord = GetRandomCoord();
             Vector3 obstaclePosition = CoordToPosition(randomCoord.x,randomCoord.y);
             Transform newObstacle = Instantiate(yellowCube, obstaclePosition + Vector3.up * .5f, Quaternion.identity) as Transform;
             newObstacle.parent = mapHolder;
         }
         
         
         
     }
 
     Vector3 CoordToPosition(int x, int y) {
         return new Vector3 (-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y / 2 + 0.5f + y);
     }
 
     public Coord GetRandomCoord() {
         Coord randomCoord = shuffledTileCoords.Dequeue ();
         shuffledTileCoords.Enqueue (randomCoord);
         return randomCoord;
     }
 
     public struct Coord {
         public int x;
         public int y;
 
         public Coord(int _x, int _y) {
             x = _x;
             y = _y;
         }
     }
 }


Comment
Add comment · Show 1
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 toddisarockstar · May 02, 2020 at 04:05 AM 0
Share

scripts are attached to gameobjects... not transforms. try useing GameObject variables for storing and spawning your prefabs.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by sath · May 02, 2020 at 07:41 AM

If the prefabs have been set up correctly this shouldn't be happening. But there is another option for that case. You can add a script to the prefab upon instantiation.

 Transform newObstacle = Instantiate(pinkCube, obstaclePosition + Vector3.up * .5f, Quaternion.identity) as Transform;
 
 Script missingScript = newObstacle.gameObject.AddComponent<Script>();
 
 //do something with this script
 //missingScript.SetCubeColor(Color.green);




Comment
Add comment · Show 1 · 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 joshocaoimh · May 02, 2020 at 10:24 AM 1
Share

You were right I wasn't using the prefab right, working now. The ability to attach a script after instantiation is also good to know. Thank you.

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

168 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

Related Questions

instantiated GameObject Prefab jumps in air after instantiate 1 Answer

How to hide a character prefab 1 Answer

Reset script to prefab values 0 Answers

Spawn At Spawnpoint 2 Answers

Hiding gameobjects in hierarchy make it less expensive ? 0 Answers


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