- Home /
script plays automatically on screen load and loops
I'm trying have this script play when the scene loads and it loops
I get error cs:1520 method must have a return type
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class titlescript : MonoBehaviour
{
public GameObject blood;
public GameObject Player;
static void OnAfterSceneLoadRuntimeMethod()
{
StartCoroutine();
}
IEnumerator ()
{
while (true)
{
yield return new WaitForSeconds(2f);
blood.SetActive(false);
GlitchCameraShader glitchCameraShader = Player.gameObject.GetComponentInChildren<GlitchCameraShader>(includeInactive: true);
if (glitchCameraShader != null)
glitchCameraShader.enabled = true;
yield return new WaitForSeconds(1.5f);
glitchCameraShader.enabled = false;
blood.SetActive(true);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class titlescript : MonoBehaviour
{
public GameObject blood;
public GameObject Player;
static void OnAfterSceneLoadRuntimeMethod()
{
StartCoroutine(Routine());
}
IEnumerator Routine()
{
while (true)
{
yield return new WaitForSeconds(2f);
blood.SetActive(false);
GlitchCameraShader glitchCameraShader = Player.gameObject.GetComponentInChildren<GlitchCameraShader>(includeInactive: true);
if (glitchCameraShader != null)
glitchCameraShader.enabled = true;
yield return new WaitForSeconds(1.5f);
glitchCameraShader.enabled = false;
blood.SetActive(true);
}
}
}
Answer by ATate · Mar 02 at 10:55 AM
You have to specify what function you want to run when you call StartCoroutine and give your function a name. The code can't be running - it never gets that far because of the compiler error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class titlescript : MonoBehaviour
{
public GameObject blood;
public GameObject Player;
static void OnAfterSceneLoadRuntimeMethod()
{
StartCoroutine(MyCoroutine);
}
IEnumerator MyCoroutine()
{
while (true)
{
yield return new WaitForSeconds(2f);
blood.SetActive(false);
GlitchCameraShader glitchCameraShader = Player.gameObject.GetComponentInChildren<GlitchCameraShader>(includeInactive: true);
if (glitchCameraShader != null)
glitchCameraShader.enabled = true;
yield return new WaitForSeconds(1.5f);
glitchCameraShader.enabled = false;
blood.SetActive(true);
}
}
}
Answer by rayith · Mar 04 at 01:19 AM
I got error CS1503: Argument 1: cannot convert from 'method group' to 'string'
StartCoroutine(MyCoroutine);
I tried to fix this StartCoroutine(MyCoroutine()); but got the error cs0120 An object reference is required for the non-static field, method, or property
This is because OnAfterSceneLoadRuntimeMethod() is a static method while MyCoroutine() is private. Static methods are shared between al instances of the same class (titlescript in this ex.), while private are members of each instance. Try changing it from static to public.
using System.Collections.Generic;
using UnityEngine;
public class titlescript : MonoBehaviour
{
public GameObject blood;
public GameObject Player;
public void OnAfterSceneLoadRuntimeMethod()
{
StartCoroutine(MyCoroutine());
}
IEnumerator MyCoroutine()
{
while (true)
{
yield return new WaitForSeconds(2f);
blood.SetActive(false);
GlitchCameraShader glitchCameraShader = Player.gameObject.GetComponentInChildren<GlitchCameraShader>(includeInactive: true);
if (glitchCameraShader != null)
glitchCameraShader.enabled = true;
yield return new WaitForSeconds(1.5f);
glitchCameraShader.enabled = false;
blood.SetActive(true);
}
}
}
You will need to pass a reference of your titlescript gameobject to the script that is calling the OnAfterSceneLoadRuntimeMethod() method.
the good news is no errors bad news nothing happened