Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Changing Texture with FindGameObjectsWithTag

Discussion in 'Scripting' started by mrttis, May 29, 2014.

  1. mrttis

    mrttis

    Joined:
    Jul 12, 2012
    Posts:
    64
    Hi,

    I want change materials texture in all same tagged objects with using "find tag". but i could not.


    using UnityEngine;
    using System.Collections;

    public class Changing : MonoBehaviour {


    public Texture newTexture ;
    // Use this for initialization
    void Start () {


    GameObject[] boxObjects = GameObject.FindGameObjectsWithTag("Box");
    foreach(GameObject box in boxObjects) {

    box.renderer.material.mainTexture = newTexture;
    }

    }

    }


    What is the problem ?
     
  2. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    Do the box objects have a MeshRenderer? Is newTexture properly set within the inspector?
     
  3. mrttis

    mrttis

    Joined:
    Jul 12, 2012
    Posts:
    64
    Yes it have mesh renderer and new texture within inspector. when i find with name there is no problem. but with find tag. not working without error
     
  4. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    Are you certaint he object tags are set correctly? and image of the inspector might help.
     
  5. mrttis

    mrttis

    Joined:
    Jul 12, 2012
    Posts:
    64
    I checked again and founded the problem. I had added findtag script to Main Camera. remove it and added an other object now. its working.
    Thank you :)