- Home /
The question is answered, right answer was accepted
how to enable or disable a script via code c#
i have a script but there is a problem :
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class enemy : MonoBehaviour {
//public Collider player_box;
public Rigidbody rb;
private Vector3 posa;
//public Transform posb;
public float speed;
private GameObject player;
public bool right;
private move_idea move_idea_script;
void Start ()
{
player = GameObject.FindGameObjectWithTag ("Player");
posa = gameObject.transform.position;
move_idea_script = GetComponent<move_idea>();
}
// Update is called once per frame
void FixedUpdate () {
if(right==false)
{
rb.velocity = Vector3.left * speed * Time.deltaTime;
}
if(right==true)
{
rb.velocity = Vector3.right * speed * Time.deltaTime;
}
}
void OnTriggerEnter(Collider other)
{
if (other.isTrigger) {
gameObject.SetActive (false);
gameObject.transform.position = posa;
gameObject.SetActive (true);
}
if (other.gameObject.tag == "Player") {
player.transform.localScale = new Vector3 (player.transform.localScale.x, 0.1f, player.transform.localScale.z);
move_idea_script.enabled =false;
//player_box.isTrigger = true;
Destroy (player, 1);
}
}
}
it compile but when i run the gun the script pop up and says gameobject not set as an instance of ..........
if any one can help meee !!!!!!!!
Please provide more information related to the error. Copy and paste the error text that appears in the unity console, it contains useful information such as the line on which the error happened.
Without that, my only guess is that you have no GameObject in your scene with a "Player" tag.
What is the exact error message that you're getting? And are you trying to disable just the script or the entire gameobject that the script is attached to? Also, can you post the move_idea script?
Answer by Kishotta · Jun 27, 2017 at 02:27 AM
Without knowing what line the null reference is on, I'd assume the Rigidbody rb
reference is causing the problem. You're not assigning it in the Start method, so unless you assign it in the inspector, you'd get a "gameobject not set" error.