• 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 NerdHerd2016 · Oct 02, 2022 at 08:57 AM · gameerror messagenew user

Need help with a error code CS0236

I'm new to programming and unity and it keeps replaying t$$anonymous$$s error code CS0236, for reference t$$anonymous$$s is my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 public class Safe_Zone : MonoBehaviour
 {
     public float limitTime;
 
  float unsafeTime = limitTime;
  bool reached;
  
  void Update () {
       if (reached) { unsafeTime = limitTime; }
       else { unsafeTime -= Time.deltaTime; }
      if (unsafeTime <= 0f) { 
         void OnTriggerEnter(Collider other)
         {
         SceneManager.LoadScene(1);
         }
     }    
  }//Do somet$$anonymous$$ng like destroy the player or show the gameover text, ... 
  void OnTriggerEnter2D (Collider2D other) { 
       if (other.gameObject.tag == "Player") { reached = true; } 
  }

  void OnTriggerExit2D (Collider2D other) { 
       if (other.gameObject.tag == "Player") { reached = false; } 
   }
      
 }

I'm trying to make where every ten seconds the player is transported into the game over scene, unless he is in a safe zone, I'm out of ideas to solve t$$anonymous$$s.

Comment
Add comment
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

1 Reply

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

Answer by Captain_Pineapple · Oct 02, 2022 at 09:09 AM

Hey there and welcome to the forum,

there is a lot to unpack here regarding your script.

The error you posted is because of t$$anonymous$$s:

   float unsafeTime = limitTime;

It is not allowed to reference another variable (here limitTime) during variable declaration unless that variable is marked as const. What you might want to do here is to assign the value of limitTime into unsafeTime during object startup.

    float unsafeTime;
    public void Awake() {
         unsafeTime = limitTime;
    }


In general, you have to make sure that you do not define functions inside of other functions. In your current version you declare void OnTriggerEnter(Collider other) (w$$anonymous$$ch is a function) inside of Update T$$anonymous$$s is not allowed.

 void Update () {
        if (reached) { unsafeTime = limitTime; }
        else { unsafeTime -= Time.deltaTime; }
       if (unsafeTime <= 0f) { 
         
       }    
   }//Do somet$$anonymous$$ng like destroy the player or show the gameover text, ... 

   void OnTriggerEnter(Collider other)
   {
          //do stuff here if a trigger-collider is $$anonymous$$t.
          SceneManager.LoadScene(1);
   }

Apart from that you should decide you your game is 2D or 3D. You can either have 2D colliders or 3D colliders. Both together should not be used. (Not sure if it is possible but i'd advise to not do it anyways). A 2D Collider will never trigger OnTriggerEnter and a 3D Collider will never trigger OnTriggerEnter2D. Look at the components you are using and remove the wrong functions from your script.


Apart from t$$anonymous$$s: Please make sure to use the code past tool when you make a post. (Little button with 1010101 on it above the text field. When you post somet$$anonymous$$ng regarding an error always make sure to post w$$anonymous$$ch line it points to -> T$$anonymous$$s is really important.

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 NerdHerd2016 · Oct 02, 2022 at 05:43 PM 1
Share

Thank you so much, ill be sure to do this from now on if I have a question!

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

226 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

Related Questions

IndexOutOfRangeException: tanks index is invalid , i was trying to add new Tank in Tanks!!! game but get this error , iam new in coding 1 Answer

IndexOutOfRangeException: Index was outside the bounds of the array at com.unity.inputsystem@1.2.0/InputSystem/Editor/AssetEditor/InputActionEditorToolbar.cs 0 Answers

Error code pop up when create new script 0 Answers

Parsing error 0 Answers

Unity Crashes When Clicking Play: Script Error? 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