# Get \_Time from shader for script

**URL:** https://discussions.unity.com/t/get-_time-from-shader-for-script/424075
**Category:** Shaders & Materials
**Tags:** Shaders
**Created:** [September 28, 2010, 12:00am UTC](https://discussions.unity.com/t/get-_time-from-shader-for-script/424075 "2010-09-28T00:00:43Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![antonbursch](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@antonbursch](https://discussions.unity.com/u/antonbursch)
#### Post date: [September 28, 2010, 12:00am UTC](https://discussions.unity.com/t/get-_time-from-shader-for-script/424075/1 "2010-09-28T00:00:43Z")

</div>

I have an ocean shader that uses a vertex sine wave to make waves on a grid mesh.

I have an object that I want to float in the ocean. I can use the exact same sine wave to get a y position for the floating object. I have done this in a shader for the floating object and it works perfect.

I have a water vehicle that I want to float in the ocean. I can use the exact same sine wave to get a y position for the floating object. But I can’t do it in a shader because I need the collision working and that gets all messed up. Therefore I need to do this using a script. And that’s simple. Except I can’t figure out how to sync up the time in the script to the time in the shader.

Script time is Time.time.

Shader time is \_Time.

They do not appear to be in sync.

I tried to use the following:

float timeY = renderer.material.GetVector(“\_Time”);

But it only returns (0, 0, 0, 0).

Any suggestions on how to sync my times between script and shaders?

Thanks

Anton

---

<div class="post-metadata">

### Author: ![reissgrant](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/reissgrant/32/430141_2.png) [@reissgrant](https://discussions.unity.com/u/reissgrant)
#### Post date: [September 28, 2010, 11:05pm UTC](https://discussions.unity.com/t/get-_time-from-shader-for-script/424075/2 "2010-09-28T23:05:30Z")

</div>

Pass the Time.time variable to the shader with:

```auto
On Update(){
  float theUnityTime = Time.time;
  myCustomMaterial.SetFloat("unityTime",theUnityTime);
}

```

Then declare this in your shader to access it:

```auto
float unityTime;

```

---

<div class="post-metadata">

### Author: ![antonbursch](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@antonbursch](https://discussions.unity.com/u/antonbursch)
#### Post date: [September 29, 2010, 10:18pm UTC](https://discussions.unity.com/t/get-_time-from-shader-for-script/424075/3 "2010-09-29T22:18:09Z")

</div>

yeah, that is what i did. it was right in front of my face too. 😜

> [@reissgrant](#):
>
> Pass the Time.time variable to the shader with:
> 
> ```auto
> On Update(){
> float theUnityTime = Time.time;
> myCustomMaterial.SetFloat("unityTime",theUnityTime);
> }
> 
> ```
> 
> Then declare this in your shader to access it:
> 
> ```auto
> float unityTime;
> 
> ```

---

<div class="post-metadata">

### Author: ![adamgryu](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/adamgryu/32/101472_2.png) [@adamgryu](https://discussions.unity.com/u/adamgryu)
#### Post date: [September 10, 2018, 8:54pm UTC](https://discussions.unity.com/t/get-_time-from-shader-for-script/424075/4 "2018-09-10T20:54:02Z")

</div>

You can get the shader’s \_Time value from a C# script by calling this:

```csharp
Shader.GetGlobalVector("_Time")

```

This way, you don’t have to modify your shaders to use a custom time variable.

---

<div class="post-metadata">

### Author: ![DearUnityPleaseAddSerializable](https://avatars.discourse-cdn.com/v4/letter/d/f1d935/32.png) [@DearUnityPleaseAddSerializable](https://discussions.unity.com/u/DearUnityPleaseAddSerializable)
#### Post date: [December 12, 2020, 2:42pm UTC](https://discussions.unity.com/t/get-_time-from-shader-for-script/424075/5 "2020-12-12T14:42:51Z")

</div>

> [@adamgryu](#):
>
> You can get the shader’s \_Time value from a C# script by calling this:
> 
> ```csharp
> Shader.GetGlobalVector("_Time")
> 
> ```
> 
> This way, you don’t have to modify your shaders to use a custom time variable.

Shader.GetGlobalVector(“\_Time”)[1] or Shader.GetGlobalVector(“\_Time”)[0]? Seems to be out of sync for me

---

<div class="post-metadata">

### Author: ![StaggartCreations](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/staggartcreations/32/610980_2.png) [@StaggartCreations](https://discussions.unity.com/u/StaggartCreations)
#### Post date: [December 12, 2020, 7:30pm UTC](https://discussions.unity.com/t/get-_time-from-shader-for-script/424075/6 "2020-12-12T19:30:35Z")

</div>

> [@DearUnityPleaseAddSerializableDictionaries](#):
>
> Shader.GetGlobalVector(“\_Time”)[1] or Shader.GetGlobalVector(“\_Time”)[0]? Seems to be out of sync for me

It depends if you are in play mode or not, otherwise EditorApplication.timeSinceStartup is what’s being fed to the \_Time parameter in shaderland. While in play mode it’s Time.time. This what I use in my buoyancy script and it matches the \_Time.x value used in shaders:

```csharp
private static float _Time
        {
            get
            {
#if UNITY_EDITOR
                return Application.isPlaying ? Time.time : Shader.GetGlobalVector("_Time").x;
#else
                return Time.time;
#endif
            }
        }

```

---

<div class="post-metadata">

### Author: ![nasos\_333](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/nasos_333/32/884371_2.png) [@nasos\_333](https://discussions.unity.com/u/nasos_333)
#### Post date: [January 24, 2024, 7:53pm UTC](https://discussions.unity.com/t/get-_time-from-shader-for-script/424075/7 "2024-01-24T19:53:32Z")

</div>

> [@DearUnityPleaseAddSerializableDictionaries](#):
>
> Shader.GetGlobalVector(“\_Time”)[1] or Shader.GetGlobalVector(“\_Time”)[0]? Seems to be out of sync for me

By Unity docs, \_Time.y is the game time, \_Time.x is the 1/20 of time.
