Is a certain way to spawn random Materials(Png created) through the game?

My issue is as follows, I can’t seem to get how to spawn random materials within random location(x,y) because all am getting is Type errors, so is there a possible way to do it in an easier way?

I have 4 Materials created to fit a cubes as “textures”(blender created) but it’s hard for me to do so.

Maybe share the code you’re trying? Sounds like you’re just getting some compile errors which should be simple enough to fix.

As Praetor noted, you’re probably just skipping some steps.

Textures don’t go into scenes.

Textures go onto Materials.

Materials don’t go into scenes either.

Materials go onto Renderers.

Without a Mesh, Renderers don’t show anything.

MeshFilters supply Meshes to Renderers.

So… you need a Mesh, in a MeshFilter, a Renderer, with a Material, optionally with Textures.

You can shortcut some of this with a SpriteRenderer, if you understand the implications.

You can also use a quad to shortcut many of the steps but you must understand what goes into what.

Thank you for replying
@Kurt-Dekker I understand most of the steps but not fully, however
@PraetorBlue Please find the code below

My goal in this code is to set an action for each material selected
for ex: Explosive gives the character self destruction power-up etc.

The error I keep getting is "
cs(6,7): error CS0138: A ‘using namespace’ directive can only be applied to namespaces; ‘Material’ is a type not a namespace. Consider a ‘using static’ directive instead
" I can’t seem to figure it out, please note am still a beginner

  using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
using UnityEngine.Material;

public class PowerUpManager : MonoBehaviour
{
 

    public Material Frwrdmat;
    public Material Backwrdmat;
    public Material Destmat;
    public Material Magmat;
    public Material LasrMat;
    public Material ExploMat;

    public class PowerUp
    {
        public enum Type
        {
            BoostFwd,
            BoostBack,
            Destruction,
            Magnet,
            Projectiles,
            Explosion,
            ExtraLife,
            Slowdown
        }

        public Type _type;
        private float _duration;
        public GameObject _object;
        public Material _material;
        private Color _color;

        public PowerUp(Type type, float duration, GameObject obj, Color color, Material material)
        {
            _type = type;
            _duration = duration;
            _object = obj;
            _color = color;
            _material = material;
        }  

private Color GetColorByType(PowerUp.Type type)
    {
        switch (type)
        {
            case PowerUp.Type.ExtraLife: return LifeColor;
            case PowerUp.Type.Slowdown: return SlowColor;
            default:
                throw new ArgumentOutOfRangeException("type", type, null);
        }
    }
    private Material GetMaterial(PowerUp.Type type)
    {
        switch (type)
        {
            case PowerUp.Type.BoostFwd: return Frwrdmat;
            case PowerUp.Type.BoostBack: return Backwrdmat;
            case PowerUp.Type.Destruction: return Destmat;
            case PowerUp.Type.Magnet: return Magmat;
            case PowerUp.Type.Projectiles: return LasrMat;
            case PowerUp.Type.Explosion: return ExploMat;
            default:
                throw new ArgumentOutOfRangeException("type", type, null);
        }
    }


private void SpawnPowerUp()
    {
        var newPowerUpY =
            RoundToMultipleOfN(_distanceBetweenPlatforms,
                Random.Range(_minSpawnY, _maxSpawnY)) +
            (float)_distanceBetweenPlatforms / 2;
        var newPowerUpParentObj = Instantiate(PowerUpParentPrefab,
            new Vector3(_spawnPosX, newPowerUpY, 0), Quaternion.identity);
        var newPowerUpObj = Instantiate(PowerUpPrefab,
            new Vector3(_spawnPosX, newPowerUpY, 0), Quaternion.identity,
            newPowerUpParentObj.transform);
        newPowerUpObj.tag = "PowerUp";
        newPowerUpParentObj.tag = "PowerUp";
        var newType =
            PowerUp.RandomType(_platformManager.PlatformSpeed,
                _playerAnimationParent);
        newPowerUpObj.GetComponent<Renderer>().material.SetColor("_Color",
            GetColorByType(newType));
        var newPowerUpDuration = GetDurationByType(newType);
        switch (newType)
        {
            case PowerUp.Type.ExtraLife:
            case PowerUp.Type.Slowdown:
                newPowerUpObj.GetComponent<Animator>()
                    .Play("Flashing");
                break;
            case PowerUp.Type.BoostFwd:
            case PowerUp.Type.BoostBack:
            case PowerUp.Type.Destruction:
            case PowerUp.Type.Magnet:
            case PowerUp.Type.Projectiles:
            case PowerUp.Type.Explosion:
            default:
                newPowerUpObj.GetComponent<Animator>()
                    .Play("PowerUpState");
                break;
        }

        newPowerUpParentObj.GetComponent<PowerUpScript>().SlowdownFactor =
            SlowdownFactor;
        PowerUps.Add(new PowerUp(newType, newPowerUpDuration,
            newPowerUpParentObj,
            newPowerUpObj.GetComponent<Renderer>().material.color));
    }

Line 6 above is the problem. It’s a class, not a namespace. Delete it.

I did try deleting Line 6, but it seems that the error i keep getting is the conversion of GetColorByType.Color to GetMaterial.

I’ll try to explain it, i’ve created the code in order to get the type of Material and store it and then calling it again under Line 74 private void SpawnPowerUp(). but i cant figure out how to convert it. so i tried including this code

newPowerUpObj1.GetComponent().material(“_Material”, GetMaterial(newType));

Still wouldnt work.

What about simply set it like this:

newPowerUpObj1.GetComponent<Renderer>().material =  GetMaterial(newType);

and remove this line because its totally wrong:

using Random = UnityEngine.Random;
1 Like