• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by Ordoplayer · Mar 03, 2019 at 02:30 PM · scripting problemshadertriggerplayerzone

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;
      }
 }
 }

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Mar 03, 2019 at 04:53 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image

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.

Comment

People who like this

0 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ordoplayer · Mar 03, 2019 at 09:12 PM 0
Share

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 : MonoBehaviour
 {
 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
 {
     [ExecuteInEditMode]
     [AddComponentMenu("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);
         }
     }
 }

avatar image Hellium Ordoplayer · Mar 03, 2019 at 10:08 PM 0
Share

Simply follow the instructions given by Unity....

 void OnTriggerEnter(Collider other)

 void OnTriggerExit(Collider other)
avatar image Ordoplayer Hellium · Mar 03, 2019 at 10:20 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

321 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to alternate character with one touch? 1 Answer

[SOLVED] Need help with my OnTriggerEnter code. 1 Answer

My AI Script doesn't make my AI turn. 1 Answer

OnTriggerEnter2D being called 25 times. 0 Answers

Player not moving in the right direction instantly 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges