Hi guys,
I am not a good unity scripter and need help. I have completed a lot of tasks myself but really need help with this one.
For my own hobby/project I am using a light gun by Aimtrak.
What I want to achieve is to have 8 objects randomly change texture from e.g. brick to any of the other two concrete or wood textures and not have it cycle through the chosen textures.
So all objects start with one texture and will change to either one of the two other textures randomly, once all the objects with the wood texture is shot the process then repeats from the start.
What I have completed myself:
I currently have the objects cycle through chosen textures but unsure how to have it stop cycling through.
I also coded - if an object is shot using “tag” names +1 hit point if missed +1 miss point
and other unimpressive stuff.
I haven’t a clue.
I don’t know if I correctly understood your problem, but you can try with this :
EDIT : I didn’t test this code, I wrote it in a notepad, thus, not sure it works.
// C#
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Foo : MonoBehaviour
{
// Add your textures from the inspector
public Texture[] Textures ;
// List of indices to get random texture
public List<int> indices ;
// Material component
public Material material ;
void Awake()
{
material = GetComponent<Renderer>().material ;
indices = new List<int>();
}
public void ApplyRandomTexture()
{
// Fill indices if every index has been used
if( indices.Count == 0 )
FillIndices() ;
// Get randomly a remaining index to get texture
int index = indices[ Random.Range( 0, indices.length - 1 ) ] ;
// Change main texture
material.mainTexture = Textures[index] ;
// Remove index to make sure the texture won't be reused
indices.Remove( index ) ;
}
void FillIndices()
{
int maxIndex = Textures.Length ;
for( int index = 0 ; index < maxIndex ; ++index )
{
indices.Add(index) ;
}
}
}
Unity Material
Unity Random.Range
MSDN List