Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by Xcaliber407 · Apr 26, 2018 at 04:57 AM · scripting problem

Is this possible? OnTriggerEnter with a delay that affects the color of an gameObject.

Hello,

I'm not sure if I understand what's going wrong with my script or if what I'm trying to do is even possible. Sorry for the messy script I was trying a few things i thought would fix it.

Basically I want the gameObject that is detected during OnTriggerEnter to change color after a certain amount of time. However and I believe it has something to do with Collider part of OnTriggerEnter; the script errors out saying line 39 in fixedupdate has null reference. meatCollision.gameObject.GetComponent().material.color = mycolor;

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GrillFunction : MonoBehaviour
 {
 
     public Color mycolor;
     private bool trigger;
     public float cookTime;
     private Collider meatCollision;
 
 
     public void Start()
     {
         meatCollision = null;
     }
 
     public void OnTriggerEnter(Collider meatCollision)
     {
 
         if (meatCollision.CompareTag("RawMeat"))
         {
             Debug.Log("This is Raw meat" + meatCollision.name);
             StartCoroutine(Example());
 
         }
 
     }
 
     private void FixedUpdate()
     {
         if (trigger == true)
         {
             Debug.Log("It became true for fixed update");
             meatCollision.gameObject.GetComponent<Renderer>().material.color = mycolor;
             
         }
 
     }
 
     IEnumerator Example()
     {
         trigger = false;
         yield return new WaitForSeconds(cookTime);
         trigger = true;
         Debug.Log(trigger);
 
 
     }
 
 }

I believe my issue revolves around OnTriggerEnter collider not being available outside the function because the color changes when the code is inside the function but never outside. Any help would be greatly appreciated. Sorry if I missed any information

Thanks.

Comment
Add comment · 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 shadowpuppet · Apr 26, 2018 at 07:07 AM 0
Share

and why FixedUpdate? no physics are involved and I thought fixedUpdate was more "expensive" than a simple Update. and I am wondering what you drag into the public variable of "mycolor". sorry , more questions than answers. I do a few color changes and they don't seem as efficient as your script. Them again, $$anonymous$$e work, but.....................bad code

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by dakshesh1010 · Apr 26, 2018 at 06:23 AM

The private meatCollision variable is always null in your case. When you call that in FixedUpdate, it still remains null. The meatCollision inside the OnTriggerEnter is just the argument you are passing, which is local to that function. You might want to assign the global meatCollision at this point. I would recommend changing the argument's name in OnTriggerEnter and assign meatCollision with the value of the argument in case it has the desired tag. Your code should look something like this:

 public void OnTriggerEnter(Collider other)
      {
          if (other.CompareTag("RawMeat"))
          {
              //Debug.Log("This is Raw meat" + meatCollision.name);
              meatCollision = other;
              StartCoroutine(Example());
          }
      }

Now when in FixedUpdate, when you change it's color, it will not throw Null exception.

Comment
Add comment · Show 1 · 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 Xcaliber407 · Apr 26, 2018 at 04:27 PM 0
Share

Awesome answer. $$anonymous$$akes much more sense as I was thinking once it collided, 'other' was now set as the object it collided with. Thank you for your answer. I was overthinking it for sure.

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.

Follow this Question

Answers Answers and Comments

200 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

Related Questions

Unity Interactive Tutorials Scripting Problem 0 Answers

SmoothDamp in my FPS controller. Does not feel like it is working. 0 Answers

If all tagged objects are destroyed 1 Answer

How To change a Text Object's Color Randomly using Color 32? 2 Answers

How to add information to an instantiated object 0 Answers


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