- Home /
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?
Try this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptOnTrigger : $$anonymous$$onoBehaviour {
public $$anonymous$$yScript _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);
}
}
}
Answer by ClumsyLobster · Apr 02, 2018 at 09:29 PM
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 ?
Answer by Dneprokos · Apr 02, 2018 at 03:41 PM
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)
What do you need to rotate? if you want to rotate the Player just get the Player transform and rotate it.
As an example
void OnTriggerExit(Collision other) { if (other.gameObject.tag == "Player") { var playerTransform = other.gameobject.GetComponent<"Transform">(); //use rotation method here. } }
Please remove quotes from Transform
no i need to rotate the fan when i enter the trigger, i have the rotate script for the fan but i don't want it to always be enable, i want to enable only when i enter that trigger
plus i want to do that to a lot more script but that's an example
Answer by augivision · Apr 02, 2018 at 09:49 PM
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 `$$anonymous$$yScript' 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 ?