Select an object, then change the texture

Hi,

I have a number of different objects and a number of textures which people can apply to those objects.

So what I would like; someone clicks (in this case, touches, since I’m using Qualcomm AR) an object, after which they can select one of the textures to apply to that object.

This works fine when I display a set of texture triggers for each of the objects seperately, but I would like to have just one set of textures which can be applied to each one of the objects.

Attached is a Mondriaan-like illustration of what I am trying to achieve.

Currently, I have these lines of code;

var Object01 : GameObject;
Object01 = GameObject.Find("Object01");
var Object02 : GameObject;
Object02 = GameObject.Find("Object02");

function Start () {

}

function Update () {

    var hit : RaycastHit;

    var touches = Input.touches;

    for (var touch : Touch in touches){

        var ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x, touch.position.y,0));

        if (Physics.Raycast (ray,hit,Mathf.Infinity)){
            
            // Object01 Texture Triggers
            
            if(hit.collider.name == ("Object01White")&& touch.phase == TouchPhase.Began){
                Object01.renderer.material.mainTexture = null;
                Object01.renderer.material.color = Color.white;
            }   
            if(hit.collider.name == ("Object01Black")&& touch.phase == TouchPhase.Began){
                Object01.renderer.material.mainTexture = Resources.Load("black-woven-carbon-fiber");
                Object01.renderer.material.mainTextureScale = Vector2 (6,6);
            }            
            if(hit.collider.name == ("Object01Red")&& touch.phase == TouchPhase.Began){
                Object01.renderer.material.mainTexture = Resources.Load("bright_red");
                Object01.renderer.material.mainTextureScale = Vector2 (8,8);
            }                 
            
            // Object02 Texture Triggers
            
            if(hit.collider.name == ("Object02White")&& touch.phase == TouchPhase.Began){
                Object02.renderer.material.mainTexture = null;
                Object02.renderer.material.color = Color.white;
            }   
            if(hit.collider.name == ("Object02Black")&& touch.phase == TouchPhase.Began){
                Object02.renderer.material.mainTexture = Resources.Load("black-woven-carbon-fiber");
                Object02.renderer.material.mainTextureScale = Vector2 (6,6);
            }            
            if(hit.collider.name == ("Object02Red")&& touch.phase == TouchPhase.Began){
                Object02.renderer.material.mainTexture = Resources.Load("bright_red");
                Object02.renderer.material.mainTextureScale = Vector2 (8,8);
            } 

        }

    }


}

As you can see, this will result in -a lot- of redundant code.

My best guess would be to replace Object01/Object02/etc with something generic which is generated by clicking one of the objects.

I think I have the pieces, now I’m just wondering how they would fit together.

Any help would be very much appreciated.

Not real code

  • Raycast on touch

  • Set object hit to a variable var selectedObject : GameObject;

  • On click texture buttons:
    selectedObject.renderer.material.mainTexture = Resources.Load(“bright_red”);
    selectedObject.renderer.material.mainTextureScale = Vector2 (8,8);

Got it!

// Define objects and selected object

var Object01: GameObject;
Object01 = GameObject.Find("Object01");

var Object02 : GameObject;
Object02 = GameObject.Find("Object02");

var selectedObject: GameObject;

function Start () {

}

function Update () {

    var hit : RaycastHit;

    var touches = Input.touches;

    for (var touch : Touch in touches){

        var ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x, touch.position.y,0));

        if (Physics.Raycast (ray,hit,Mathf.Infinity)){

           // Find out what object is hit and store in selectedObject

           if(hit.collider.name == ("Object01")&& touch.phase == TouchPhase.Began){
                selectedObject= GameObject.Find("Object01");
            }   
	       if(hit.collider.name == ("Object02")&& touch.phase == TouchPhase.Began){
                selectedObject = GameObject.Find("Object02");
            }   

            // Set texture for the selected object

            if(hit.collider.name == ("TextureWhite")&& touch.phase == TouchPhase.Began){
                selectedObject.renderer.material.mainTexture = null;
                selectedObject.renderer.material.color = Color.white;
            }   
            if(hit.collider.name == ("TextureBlack")&& touch.phase == TouchPhase.Began){
                selectedObject.renderer.material.mainTexture = Resources.Load("black-woven-carbon-fiber");
                selectedObject.renderer.material.mainTextureScale = Vector2 (6,6);
            }     
        }

    }
}

Not sure if it’s the most elegant or efficient way, but it works!

Thanks a lot for the pointer Vonni.

Hey, I’m doing something similar to this…. well actually the opposite. Where you have selected the object then set the colour, I want to select the colour then apply it to an object. Any ideas on how to achieve this? You’re code was a great starting point :slight_smile:

(Also I know this isn’t an answer but I know notifications are a bit weird for this, just wanted to make sure it s seen)