How can i activate a script on a character when entering in a trigger zone ?
Hello,
I'm new to programming so i'm having trouble with a script. I can't get my player activate a script that is on himself when he arrives in a trigger zone. i just want that, when my player arrives in a trigger area, a script that is on my player activates and work when i'm on the area, and when i'm not on that particular area stop. The script is a script that activate a shader (the script with the shader is working when i'm just putting it on my player).
Thank you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class trigger : MonoBehaviour
{
void OnTriggerEnter(Collision other)
{
if (other.gameObject.tag == "Player")
{
Script Chunky = other.gameObject.GetComponent<Chunky>();
Chunky.enabled = true;
}
}
void OnTriggerExit(Collision other)
{
if (other.gameObject.tag == "Player")
{
Script Chunky = other.gameObject.GetComponent<Chunky>();
Chunky.enabled = false;
}
}
}
What does not work? Are you sure OnTriggerXXX
functions are called (a simple Debug.Log(other.gameObject.tag);
outside of the conditions will help you find out the problem). Remember that if you use 2D components, use 2D callbacks.
Answer by Sazails · Mar 03, 2019 at 06:43 PM
Hi and welcome, For scripts you don't need "Script" in front, just write the actual script name.
Chunky.enabled = true
This is correct, however I need to see the starting of your "Chunky" script to see how you start your script.
Thank you, I replaced the "Script" by my script name, that is "trigger", now i've got another error, it's written : Script error: OnTriggerEnter This message parameter has to be of type: Collider The message will be ignored.
and
Script error: OnTriggerExit This message parameter has to be of type: Collider The message will be ignored.
Here is the actual state of my script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class trigger : $$anonymous$$onoBehaviour
{
void OnTriggerEnter(Collision other)
{
if (other.gameObject.tag == "Player")
{
trigger Chunky = other.gameObject.GetComponent<trigger>();
Chunky.enabled = true;
}
}
void OnTriggerExit(Collision other)
{
if (other.gameObject.tag == "Player")
{
trigger Chunky = other.gameObject.GetComponent<trigger>();
Chunky.enabled = false;
}
}
}
Here is the script of the "chunky" script :
using Assets.Pixelation.Example.Scripts;
using UnityEngine;
namespace Assets.Pixelation.Scripts
{
[ExecuteInEdit$$anonymous$$ode]
[AddComponent$$anonymous$$enu("Image Effects/Color Adjustments/Chunky")]
public class Chunky : ImageEffectBase
{
public Texture2D SprTex;
public Color Color = Color.white;
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
float w = Camera.main.pixelWidth;
float h = Camera.main.pixelHeight;
Vector2 count = new Vector2(w/SprTex.height, h/SprTex.height);
Vector2 size = new Vector2(1.0f/count.x, 1.0f/count.y);
//
material.SetVector("BlockCount", count);
material.SetVector("BlockSize", size);
material.SetColor("_Color", Color);
material.SetTexture("_SprTex", SprTex);
Graphics.Blit(source, destination, material);
}
}
}
Simply follow the instructions given by Unity....
void OnTriggerEnter(Collider other)
void OnTriggerExit(Collider other)
I've replaced by Collider, but when i'm on my trigger zone, nothing happen, my chunky script that is attached on my player controller isn't activated.
Your answer
Follow this Question
Related Questions
How to fix these errors when launching a built game 0 Answers
How do I make somthing happen when the Player reaches a certain x, y, z position? 0 Answers
Own player controller script let the view wobbles but why? 0 Answers
My gameobject stops moving after entering the trigger?? 2 Answers
Mirror reflection not working as planned 0 Answers