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 /
avatar image
0
Question by gilgada · Jan 05, 2012 at 01:29 PM · gameobjectinstantiatemeshcomponentprojectile

Creating more objects with code.

I am trying to set up a script that will let me create new projectiles for my catapult to fire.

Eventually this will be limited to one at a time and will let the player choose between three different materials with different weights. However, I do not need to worry about this just yet.

When I press the right button to trigger the script, I can see the scroll bar in the Hierarchy moving and getting smaller, so there are obviously new entries being added. The big question for me is, why can't I see these new objects? I believe I am putting them on a visible part of the screen for my static camera to view. Am I missing a vital component?

My code is as follows:

var projectile : GameObject; var rb : Rigidbody; var mr : MeshRenderer; var bc : BoxCollider;

function Update(){

if(Input.GetKeyDown (KeyCode.Q)) { projectile = new GameObject ("Projectile"); projectile.tag = "projectile";

bc = projectile.AddComponent("BoxCollider"); projectile.collider.center = Vector3.zero;

mr = projectile.AddComponent("MeshRenderer");

rb = projectile.AddComponent("RigidBody"); projectile.rigidbody.isKinematic = true;

projectile.transform.position = Vector3(0,1,0); } }

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 cj_coimbra · Jan 05, 2012 at 01:40 PM 0
Share

As soon as you press the key, pause the game and zoom into one of these objects (in the Editor) to see if all components are O$$anonymous$$ and if they didn´t just fall down after you created them (because of their RigidBody).

avatar image gilgada · Jan 05, 2012 at 02:09 PM 0
Share

Thanks I just did this. The objects are staying put because they are kinematic. The problem is all I can see is the green frame around them and no actual texture.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by hotemup · Jan 05, 2012 at 07:48 PM

Your GameObject has no mesh. The mesh tells the computer what the GameObject is shaped like and such. So you are making a GameObject, but the computer doesn't know what you want it shaped like so it cannot render it, and also cannot figure out where the bounds of the collider should be. You would have to add a Mesh Filter and a mesh to that mesh filter for the computer to figure it out.

Also, instead of reconstructing the exact same object each time, it is easier to make a prefab in the inspector and use GameObject.Instantiate.

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 gilgada · Jan 18, 2012 at 07:59 AM 0
Share

the object still shows up blank, even after defining the mesh filter in the inspector view of the script under the gameObject it is attached to.

avatar image
0

Answer by Dreamside · Jan 18, 2012 at 09:16 AM

Probably your mesh renderer does not have any material attached and also does not have a mesh filter.You should check these

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

7 People are following this question.

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

Related Questions

AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 Answers

Weird Glitch When Instantiating a Game Object 1 Answer

Cannoot change variable of Component after Instantiating GameObject 0 Answers

Instantiating a Rigidbody instead of a GameObject 1 Answer

Instantiate does not return 3 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