Sprite Shadow Script Problem

Hello.
I’ve been using this script to cast dynamic shadows with sprites for a while now.
However, with one of the new updates, it has become outdated, and needs something to be fixed.

using UnityEngine;
using System.Collections;

public class SpriteShadows : MonoBehaviour {

	// Use this for initialization
	void Start () {
		Renderer renderer = GetComponent <Renderer>();
		if (renderer == null)
			Debug.Log ("Renderer is empty");
		GetComponent<Renderer>().shadowCastingMode = 
				UnityEngine.Rendering.ShadowCastingMode.On;
		GetComponent <Renderer>().receiveShadows = true;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

When I go to test, I get the error message “GetGfxDevice() should only be called from the main thread”
Followed by a crash.
Can anyone point me in the right direction to fix this and get the script up to date?

2 Answers

2

I have tested your code and worked fine for me, are you using threads? cause from threads you cant call most of unity methods, and thats what your error is saying, but Start method is never going to be call from a thread so it is a bit extrange.

@xxmariofer
Hmm.
What shader are you using for the sprite?
Beforehand I had to use a legacy shader to make this work,
but something may have changed in the update.
I don’t believe I’m using threads. Can I see a screenshot of how you have it set up?