Webcam on Multiple Objects

Hi I’m a design student working on a project for university where I want to be able to use a webcam as a material across multiple objects. I’ve found various suggestions of how to do this across different forums, and have got a webcam on a single object up and running, but can’t get it onto multiple objects. I’m new to c# and have started to learn it but still know pretty much nothing. My project deadline is looming and I’d really appreciate if anyone that’s feeling generous might be able to help me out. Thanks a lot!

No worries, solved it myself :slight_smile: If anyone else without much code experience like me wants to do this, use the below c# script and create a folder called Resources, and inside that, a material called webCamTex

Then drag the script and the new material onto one of your objects, and drag the webCamTex material onto all other objects you want the webcam to show up on.

big thanks to @Spyrai on this answer for the majority of the solution: Use the same webcamtexture on multiple objects - Questions & Answers - Unity Discussions

using UnityEngine;

public class WebCamScript : MonoBehaviour
{
    private WebCamTexture _webcamTexture;
    private Renderer _renderer;
    // Assign the Material you are using for the web cam feed
    [SerializeField] private Material webCamTex;





    void Start()
    {

        webCamTex = Resources.Load("webCamTex", typeof(Material)) as Material;

        // Grabbing all web cam devices
        WebCamDevice[] devices = WebCamTexture.devices;

        // I just use the first one, use which ever one you need 
        string camName = devices[0].name;

        // set the Texture from the cam feed
        WebCamTexture camFeed = new WebCamTexture(camName);

        // Assign the materials texture to the WebCamTexture you made,
        // this applies it to all objects using this Material
        webCamTex.mainTexture = camFeed;

        // Then start the texture
        camFeed.Play();

        gameObject.GetComponent<Renderer>().material = webCamTex;

    }
}

This solved my problem, thank you! I initially put the script and material on every object by mistake. Must remember to have one script in the scene and then attach the material to everything else! Thanks again!