Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 kharon · Nov 03, 2013 at 09:24 PM · javascriptinstantiatetransform

Building a wall

Unity 101

I'm currently working on a small script as part of a larger project. I'm trying to write a simple script to build a wall of blocks along a set axis using Cube shapes.

The final outcome would control placement and position of each newly created object on a 3D plain. At the moment I'm testing this on a 2D plane.

alt text

Example image: Initial Design | Framework

I'm trying to create a set of new objects (or prefabs) after each object stack, and limit this to the maximum width (x axis) of my Floor object. I would also like to be able to delete each object on key press - ultimately I would like objects to be removed and added at any time.

My question is: How would I go about implementing Destroy() on my Transform object?

N.B. I'm new to Unity and programming.

MY CODE:

 #pragma strict
 
 var brick : Transform;
 
 // Set x and y variables
 
 var y : int = 1;
 var x : int = 0;
 
 function Start () {
 
 }
 
 function Update () {
     
     // If spacebar pressed create new object
     if (Input.GetKeyDown ("space")) {
     
         Instantiate(brick, Vector3(x,y,-4), Quaternion.identity);
         
         // Increase y value and place new object on top of previous object
         y = y + 1;
         
         // If y is greater than set value
         // Return y value to zero and increase x value
         if (y > 3) {
             y = 1;
             x = x + 1;
             print("x = " + x + " " + "y = " + y);
             
             // If x is greater than Floor width set x value back to zero
             if( x > 9) {
                 x = 0;
             }
         } 
      }
     
     // If left ctrl press, delete brick object
     if(Input.GetButtonUp("Fire1")){
         Destroy(brick);
     }
 }
     
     
     


brix.png (6.6 kB)
Comment
Add comment · Show 2
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 Freaking-Pingo · Nov 03, 2013 at 10:55 PM 0
Share

Pleas clarify, what is your question?

avatar image kharon · Nov 03, 2013 at 11:09 PM 0
Share

I suppose I have two questions, but my main question is this:

  • would like to know how to remove or Destroy() objects in my script. I'm trying to get it so that I can control the number of objects (Cubes) in my Scene - I'm essentially creating columns of cubes in my Game to simulate a procedural wall effect.*

Note: At the moment I keep receiving: "Destroying assets is not permitted..." error.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Freaking-Pingo · Nov 03, 2013 at 11:18 PM

I think the problem arises when you try to run Destroy(brick). Your prefab is assigned to brick in the Unity Inspector, which is simply a reference to the prefab in your local assets. The problem is, you are actually trying to "`Destroy()`" the local prefab in your Unity assets. Unity is just so nice and friendly, not letting you do that.

Instead, you should store the Instantiated gameobject into a GameObject variable, such as:

 var newBrick : GameObject = Instantiate(brick, Vector3(x,y,-4), Quaternion.identity);

If you wan't to destroy this brick in the future, you should use Destroy(newBrick).

Comment
Add comment · Show 8 · 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 kharon · Nov 04, 2013 at 12:20 AM 0
Share

Ah, O$$anonymous$$, this is making sense.

I'm pretty much using examples from the Unity script resources. $$anonymous$$aybe you could help me understand something.

At the moment I'm creating a Transform prefab, and I get error:

 "...can't convert 'UnityEngine.Transform' to 'UnityEngine.GameObject'"

When I change my brick var to GameObject (at the start of the script - from Transform) I lose control of translations.

I think I'm misunderstanding how prefabs and Transform work.

avatar image LukaKotar · Nov 04, 2013 at 12:25 AM 0
Share

One way of doing it would be to change the variable type to Transform:

var newBrick : Transform = Instantiate(brick, Vector3(x,y,-4), Quaternion.identity);

And then you can destroy the object like this:

Destroy(newBrick.gameObject);

avatar image kharon · Nov 04, 2013 at 12:50 AM 0
Share

Thanks, seems to work, but I'm getting an error to assign instance reference:

 "Object reference not set to an instance of an object"


I'm looking at the Destroy() reference. I take it that I need to keep track of each instance created to call destroy. I'm not clear on how to get around this.

Source reference: http://unity3d.com/learn/tutorials/modules/beginner/scripting/destroy

avatar image Freaking-Pingo · Nov 04, 2013 at 07:11 AM 0
Share

"Object reference not set to an instance of an object" is an error that often occurs when you are trying to use or access a variable like Transform or GameObject that is empty (No values assigned to it).

An example of this case would be Destroy(newBrick.gameObject); where newBrick haven't been assigned with any value.

avatar image kharon · Nov 04, 2013 at 10:50 AM 0
Share

I think I'm misunderstanding how instances work. I have a prefab assigned to my Script attached to an Empty GameObject. Calling Transform object with Instantiate creates a clone of my prefab. I'm not sure where I'm not assigning newBrick i.e.

 var newBrick : GameObject = Instantiate(brick, Vector3(x,y,-4), Quaternion.identity);

I've tried this with a single GameObject (Cube) instance. I think I'm misunderstanding how GameObjects work entirely.

References:

  1. http://docs.unity3d.com/Documentation/$$anonymous$$anual/InstantiatingPrefabs.html

  2. http://www.unity3dstudent.com/2010/07/beginner-b05-instantiate-to-create-objects/

  3. http://www.unity3dstudent.com/2010/07/beginner-b04-destroying-objects/

  4. http://unity3d.com/learn/tutorials/modules/beginner/scripting/destroy

Show more comments
avatar image
0

Answer by steveh2112 · Dec 24, 2016 at 11:47 PM

i wrote a script to generate a brick wall that allows for configurable height, width, randomness of brick width, tapering in height and random material.

here is the script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 
 public class BrickWall : MonoBehaviour {
 
     public AudioClip audioClip;
 
     public GameObject source_brick;
     public Material[] materials;
     public int n_high = 10;
     public int n_wide = 0;
     public float width = 100;
     public Vector3 brick_size = new Vector3(.7f, .5f, 5);   // this is the basic brick size, can me modified by brick_random_x_max and brick_top_row_z
     public float brick_random_x_max = 1.4f; 
     public float brick_top_row_z = .5f;
     public Vector2 gap;
     public bool bottomRowKinematic;
 
     // Use this for initialization
     void Start() {
 
         Time.timeScale = 1;
 
         Vector3 pos = Vector3.zero;
         Quaternion rotation = Quaternion.identity;
         // mother should i build a wall
 
         // pre calc widths
         for (int y = 0; y < n_high; y++, pos.y += brick_size.y+gap.y)
         {
             Vector3 localScale = brick_size;
             if (brick_top_row_z != 0) {
                 localScale.z = Mathf.Lerp(brick_size.z, brick_top_row_z, (float)y / n_high);
             }
 
             pos.x = 0;
             for (int x = 0 ; (n_wide==0) ? pos.x < width : x < n_wide ; x++)
             {
                 // build a row
                 if (brick_random_x_max != 0)
                 {
                     // randomize width
                     localScale.x = Random.Range(brick_size.x, brick_random_x_max);
                 }
 
                 Vector3 place_pos = transform.position + pos;
                 place_pos.x += localScale.x / 2;    // alling left size
 
                 GameObject brick = Instantiate(source_brick, place_pos, rotation, transform);
                 brick.transform.localScale = localScale;
                 Renderer rend = brick.GetComponent<Renderer>();
                 if (rend != null)
                 {
                     rend.material = materials[Random.Range(0, materials.Length-1)];
                 }
                 brick.name = "brick_" + x.ToString("D2") + "_" + y.ToString("D2");
                 pos.x += localScale.x + gap.y;
 
                 if(y==0)
                 {
                     if(bottomRowKinematic)
                     {
                         Rigidbody rb = brick.GetComponent<Rigidbody>();
                         if (rb != null)
                             rb.isKinematic = true;
                     }
                 }
             }
         }
     }
 }

to use it, create an empty object for your wall and attach this script. then create a cube (i guess any object will work) and make the scale 1,1,1. attach rigid body if you care about the physics. in the script, drag the cube to the source_brick slot drag your materials to the materials list (1 is ok) set the brick size if you set brick_random_x_max and public float brick_top_row_z to 0 it will be a standard brick wall brick_random_x_max gives you more of a stone wall effect brick_top_row_z lets you make a pyramid but in z direction only

i used this to make a dam wall

enjoy

PS, it doesn't scale or rotate. scaling you can take care of by changing the brick size parameter but it need rotation adding. maybe i'll add that when i have time

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

18 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

Related Questions

Cannot set parent for instantiated GameObject. (Exception: Can't destroy Transform component of...) 0 Answers

If two Vector2 are same 1 Answer

Unity3d 4.6 javascript prefab with script 1 Answer

Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. 1 Answer

Having trouble Instantiating. 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