trying to enable script on trigger enter

Hi,

I’m trying to make this code but it always give me error
I want to enable script on object “fan” when i enter a trigger

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptOnTrigger : MonoBehaviour {

	public GameObject MyScript;

	void OnTriggerEnter(Collision other)
	{
		if (other.gameObject.tag == "Player")
		{
			MyScript.GetComponent<GameObject>().enabled = true;
		}
	}

	void OnTriggerExit(Collision other)
	{
		if (other.gameObject.tag == "Player")
		{
			MyScript.GetComponent<GameObject>().enabled = false;
		}
	}

}

but it always give me

ScriptOnTrigger.cs(21,40): error CS1061: Type `UnityEngine.GameObject' does not contain a definition for `enabled' and no extension method `enabled' of type `UnityEngine.GameObject' could be found. Are you missing an assembly reference?

3 Answers

3

Hi,
I’m not sure I fully understand your problem but looks like you’re trying to take GameObject type as a component.

Please try this one:

Add your script name instead of GameObject as Component type.

“MyScript.GetComponent<“ScriptName”>().enabled = false;”

Hi, Thank you for your comment what i'm trying to do is when the player enter a trigger a script on the "fan" pref to enable the "rotate around" script that's attracted to it . I tried yours but i give me this error now ScriptOnTrigger.cs(21,13): error CS0305: Using the generic method UnityEngine.GameObject.GetComponent<T>()' requires 1' type argument(s)

–

I find the properties for my script in the inspector missing while the scene is switched during the gameplay. How can I fix this ?

–

The script attached to the trigger needs to do 2 things. 1) Reference the GameObject with the script you want to enable/disable and 2) specify the component of the gameobject (in this case it’s a script called “NameOfScript”) you want to enable.

// reference the fan GameObject
public GameObject FanGO;

void OnTriggerEnter(Collision other) {

    // enable script on GameObject when player enters trigger
    FanGO.GetComponent<NameOfScript>().enabled = true;

}

“Type UnityEngine.GameObject’ does notcontain a definition for enabled”

Unity is telling you what you are doing wrong; specifically you are trying to call a non-existent function on a Base class used for all entities in Unity scenes.

Hi, Thank you for the reply and sorry for late reply on my part I didn't work at first but then unity told me to change Collision to Collider and it worked :D so thank you so much for the code but since i'm codding noob i don't know what that did or will this affect the trigger when anything else except the player enter ? i add if (other.gameObject.tag == "Player")anyway !!?? Plus is there a way to add the script name in unity inspector rather than write it myself every time in the script itself when i want to change it ?

–

The components on the GameObjects are not persistent between scenes, if you load an other scene all the GameObjects are destroyed, and the new ones are loaded. That might cause the impresion that the values ate getting lost, i reccomend you to look up the DontDestroyOnLoad method, examples and it's usage, but that topic is way too far of the scope for this question.

–

Try This


  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class ScriptOnTrigger : MonoBehaviour {
  
      public MyScript _myScript;
  
      void OnTriggerEnter(Collision other)
      {
          if (other.gameObject.tag == "Player")
          {
              other.GetComponent<_myScript>().SetActive(true);
          }
      }
  
      void OnTriggerExit(Collision other)
      {
          if (other.gameObject.tag == "Player")
          {
              other.GetComponent<_myScript>().SetActive(false);
          }
      }
  
  }

Make sure one of the objects has a Rigidbody attached.

Hi thank you so much for your reply i think your code summers it perfectly but every time i enter it, it give me this error error CS0246: The type or namespace name `MyScript' could not be found. Are you missing an assembly reference? and is there away to specify the game object like if i add public GameObject object; object.GetComponent<_myScript>().SetActive(true); well it work ?

–