Question by
Reality-Gaming-Studio · Jul 22, 2016 at 08:49 AM ·
c#uierrortogglenon-static
error CS0120: An object reference is required to access non-static member `UnityEngine.UI.Toggle.isOn'
Hi, I'm having this error: error CS0120: An object reference is required to access non-static member `UnityEngine.UI.Toggle.isOn' And here's the code.
using UnityEngine;
using UnityEngine.UI;
using MaterialUI;
using System.Collections;
using System.Collections.Generic;
public class Manager : MonoBehaviour {
[SerializeField]
private Image m_ColorImage1;
[SerializeField]
private AnimationCurve m_AnimCurve;
private string currentLevel = "";
public Text scoreText;
public bool isOn;
public float score;
public void Link(string url) { Application.OpenURL(url); }
void Start(){
score = 0;
SetScoreText ();
scoreText.text = "0";
}
public void LoadScene(string scene)
{
Application.LoadLevel(scene);
}
void SetScoreText(){
scoreText.text = score.ToString();
}
public void ToggleAddScore(string num){
if (Toggle.isOn = true){
score += 1;
SetScoreText();
}
Toggle.isOn = false;
}
public void QuitApp()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
I searched alot, but not worked..
Comment
Best Answer
Answer by saschandroid · Jul 22, 2016 at 09:16 AM
If this script is on a toggle button: replace Toggle.isOn
with GetComponent<Toggle>().isOn
Or:
// Assign in the inspector
public Toggle m_toggle;
and use
m_toggle.iOn
or:
// Assign in the inspector
public GameObject m_toggle;
and use
m_toggle.GetComponent<Toggle>().isOn