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 godfreyidk · Sep 20, 2018 at 10:21 AM · renderer

How to render a constructor-created gameObject

Hello Community,

Quick noob question here. My plan was to quickly create a game object via script using the new GameObject() constructor. My motivation for using the constructor instead of instantiating a prefab, is because of the game object being simplistic, only to be visible and have a transform. I didn't want to create a prefab and add a public/serialized variable to the script for dropping in the prefab. But after creating the game object with the code below, it turns out the object (a supposed sphere) remains 'invisible'. How do i make the object visible in the scene? Here's the code, and thank you for reading.

 GameObject blueSphere;
 
 viod Update()
 {
     if (blueSphere == null)
     {
         blueSphere = new GameObject("BlueBall");
         
         blueSphere.transform.position = Vector3.zero;
         
         blueSphere.transform.localScale = Vector3.one;
         
         blueSphere.AddComponent<MeshRenderer>();
         blueSphere.GetComponent<MeshRenderer>().enabled = true;
         blueSphere.GetComponent<MeshRenderer>().material.color = Color.cyan;
     }
 }
 
 
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

1 Reply

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

Answer by Hellium · Sep 20, 2018 at 10:47 AM

If you read your code :

  1. You create an empty

  2. You set its position and scale

  3. You add a MeshRenderer and apply a color

But even if you add a MeshRenderer, you never tell what mesh to render.

In your case, if you just want a Sphere, use the GameObject.CreatePrimitive function:

 if (blueSphere == null)
 {
     blueSphere = CreateSphere( "BlueBall", Vector3.zero, Vector3.zero, Vector3.one, Color.cyan );
 }

 // ...
 
 private GameObject CreateSphere( string name, Vector3 position, Vector3 rotation, Vector3 scale, Color color )
 {
     GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere
     sphere.name = name ;
     sphere.transform.SetPositionAndRotation( position, Quaternion.Euler( rotation ) );
     sphere.transform.localScale = scale ;
     sphere.GetComponent<MeshRenderer>().material.color = color;
     Component.Destroy( sphere.GetComponent<Collider>() ) ; // If you don't want the collider automatically attached
     
     return sphere ;
 }

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 godfreyidk · Sep 20, 2018 at 10:51 AM 0
Share

Hello Hellium,

Thank you, that solves it.

Cheers

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

153 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

Related Questions

Renderer.isVisible returning true with back side of Quad Primitive 0 Answers

Issue where Fading out Game Object causes screen to blank and then reload with gameobject gone 0 Answers

Material disappears after LoadScene 1 Answer

material.SetColor causing Hidden/InternalErrorShader 1 Answer

Rotating and lerping to another material with custom shader. 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