Skip to content
TheCell's Blog
  • Home
  • Portfolio
  • Electronics and Programming

Create Object and Material at Runtime with Unity 2017+

On 9. July 2018 by TheCell

A quick guide on how to create Objects and Materials at Runtime. I use it for debug objects etc. This is how it looks like when I use it:


// creating a Primitive
GameObject debugSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
debugSphere.name = "debugSphere";
// set the position to your objects position and scale it down
debugSphere.GetComponent().position =
new Vector3(
transform.position.x + xPosition,
transform.position.y + 0,
transform.position.z + zPosition);
debugSphere.GetComponent().localScale = new Vector3(0.2f, 0.2f, 0.2f);
// create a temporary Material and color it
Material debugMaterial = new Material(Shader.Find("Standard"));
debugMaterial.color = new Color(1.0f, 0.7f, 0.0f);
debugSphere.GetComponent().material = debugMaterial;

For what you can choose as PrimitiveType see https://docs.unity3d.com/ScriptReference/PrimitiveType.html (At the point of writing it was: Sphere, Capsule, Cylinder, Cube, Plane, Quad)

For what you can choose as Material (Standard etc.) create a material and click on the Shader dropdown.
For Shaders in Subfolders (ex. FX) you write “FX/Water” etc.

Gamedev
Tags: color, Material, Object, Runtime, Script, unity

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Categories

  • Allgemein
  • Fotos
  • Gamedev
  • games
  • HSLU
  • ImpressionsFrom
  • KVD
  • Papers
  • Persönlich
  • Portfolio
  • Programming
  • Zitate

Recent Posts

  • Unity EditorScripting
  • One way to handle and save Enums
  • The Impressions from Bugsnax
  • The Impressions from Manifold Garden
  • The Impressions from Sable

Archives

  • November 2023
  • December 2022
  • November 2022
  • February 2022
  • November 2021
  • October 2021
  • September 2021
  • July 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • September 2020
  • July 2020
  • April 2020
  • March 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • June 2019
  • February 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • May 2018
  • March 2018
  • February 2018
  • December 2017
  • November 2017
  • September 2017
  • July 2017
  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • October 2016
  • September 2016
  • July 2016
  • May 2016
  • April 2016
  • March 2016
  • August 2015
  • July 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • October 2014
  • April 2014
  • March 2014
  • February 2014

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright TheCell's Blog 2025 | Theme by Theme in Progress | Proudly powered by WordPress