What is wrong with my script to change shader on all GameObjects?

I wrote this script so that I wouldn’t have to change each material manually, but so that the script would do the work for me.

However it’s not working. And yes, I have the shader assigned in the inspector.

using UnityEngine;
using System.Collections;

public class ToonMaterialScript : MonoBehaviour {

	// Array to store all the GameObjects in the scene
	GameObject[] AllGameObjects;
	// Shader assigned in the inspector
	public Shader Shader;

	// Use this for initialization
	void Start () {
		
		// Find All GameObjects
		AllGameObjects = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];

		// Loop through all GameObjects
		foreach (GameObject go in AllGameObjects) {
			// If the Gameobject has a material
			if (go.GetComponent<Material>()) {
				// Get the material of the gameobject
				Material mat = go.GetComponent<Material> ();
				// Set the shader of the material of the gameobject
				mat.shader = Shader;
			}
		}
	}
}

You cannot directly change the Material used to render an object. Instead you can access it via the Renderer component.

Something like this :

foreach (GameObject go in AllGameObjects) {
             // If the Gameobject has a material
             if (go.GetComponent<Renderer>()) {
                 // Get the material of the gameobject
                 Material mat = go.GetComponent<Renderer> ().material;
                 // Set the shader of the material of the gameobject
                 mat.shader = Shader;
             }
         }