- Home /
Duplicate Question
CS0119 Error for Script Disable?
Hello, I've been working on a script to disable a script in one scene, but to keep it enabled on another. However, I've been getting an error which I thought was to do with constructors, not scripts. I've tried adding new in front of GetComponent, but that ended up giving a CS0246 error instead.
using UnityEngine;
using System.Collections;
public class Reset : MonoBehaviour
{
public Alive alive;
void Start()
{
alive = GetComponent(Alive);
}
void Update()
{
if (Input.GetKeyDown("z"))
{
alive.enabled = true;
Application.LoadLevel(1);
}
if (Input.GetKeyDown("x"))
{
alive.enabled = false;
Application.LoadLevel(0);
}
}
}
The error is:
Assets/Reset.cs(10,38): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
You can use the Generic version:
alive = GetComponent<Alive>();
Answer by guitarxe · Dec 17, 2013 at 05:46 PM
Need different syntax for C#
alive = GetComponent<Alive>();
Might need to still cast it as Alive
This question has example of GetComponent
in C# http://answers.unity3d.com/questions/550578/cant-understand-getcomponent-c.html
Oh hold on, C# uses a different syntax for this. I'll edit the answer.
$$anonymous$$anaged to get the issue resolved in the end after a bit more digging, I should have probably mentioned I'm doing this in v2.5.
And now I've run right into another issue, namely that Alive is an Awake function (DontDestroyOnLoad, to be precise). But that's a result of my own ignorance, not your script, so I guess this is solved. Hopefully its useful for others at least. I'll probably try to use an If statement in the Awake function ins$$anonymous$$d, using Application.loadedLevel.
Follow this Question
Related Questions
Enable/Disable GameObject Button script 1 Answer
Unable to enable script 0 Answers
Re Enable C# script on camera 1 Answer