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 Jan182 · Aug 25, 2015 at 06:07 PM · c#javascriptscripting problemenable and disable script

How to enable a script in Play-Mode?

Hey there.

First of all I'm quite a noob, so please be patient with me....

I'm currrently working on a jump game quite similiar to Doodle Jump.

My problem is that I want to enable a specific script as soon as the game character passes the same line as where a platform is located. So it doesn't have tobe the case that the player gets in touch with the platform. I tried to get and compare the position coordinates of the two objects and then wanted to check if they are equal and if this was the case, the script should be enabled. I've spent lots of time with it already, but I still don't have a solution for this.

So please please help me!

Here is the code that should enable the other script:

 using UnityEngine;
 using System.Collections;
 
 public class enableScript : MonoBehaviour {
     private Transform othertransform;
     private Transform player;
 
     void Awake() { 
         float othertransform = GameObject.FindWithTag("MainCamera").transform.position; 
         float player = GameObject.FindWithTag ("Player").transform.position; 
     }
 
     void Update () {
         if (othertransform ==  player) {
         (gameObject.GetComponent( "DestroyPlatform" ) as MonoBehaviour).enabled = true;
         }
     }
 
 }
 

Then this is the code that should be enabled:

 #pragma strict
 
 var othertransform : Transform;
 
 function Awake() { 
     othertransform = GameObject.FindWithTag("MainCamera").transform; 
 
 }
 
 function Update () {
     if (Vector3.Distance(transform.position, othertransform.position) > 20) { 
         Destroy(this.gameObject, 5);
         
     } 
 }

and finally this is what the interface of the game looks like (the camera follows the player just on y-axis btw):

alt text

Thank you a lot for your help in advance!

sample1.jpg (28.5 kB)
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
1
Best Answer

Answer by Vice_Versa · Aug 25, 2015 at 06:58 PM

instead of "enabling" the script, you should just write the code you need into a function, then call that function when the player touches the area. what you can do for that is put a large invisible object in that area, make sure the colllider is on and isTrigger is checked, then in a script on that object, you can use OnTriggerEnter() to call that above mentioned function. make sense?

Edit: in your case actually what you should do is have a boolean variable set to false, then it gets set to true in the OnTriggerEnter function. Then have an if statement in the update function so that it wont execute that code unless the boolean variable is true.

Comment
Add comment · Show 3 · 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 Jan182 · Aug 27, 2015 at 10:09 PM 0
Share

firstly thank you for the response! secondly I think I understand what you mean and I tried to write the code like this but it still doesn't work. probably I made mistakes in the code. so here is it, maybe you can see what I did wrong:

 #pragma strict
 
 var DestroyPlatform : DestroyPlatform;
 public var checkenter : boolean = false;
 
 function Start () {
 
 }
 
 function Update () {
     OnTriggerEnter();
     if (checkenter == true) {
         DestroyPlatform.Update();
         
     }
 }
 
 function OnTriggerEnter () {
     checkenter = true;
     
 }

and the DestroyPlatform script is still the following:

 #pragma strict
 
 var othertransform : Transform;
 
 
 function Awake() { 
     othertransform = GameObject.FindWithTag("$$anonymous$$ainCamera").transform; 
 
 }
 
 
 function Update () {
     if (Vector3.Distance(transform.position, othertransform.position) > 20) { 
         Destroy(this.gameObject, 5);
         
     } 
 }
 
 

avatar image Vice_Versa · Aug 28, 2015 at 01:18 AM 1
Share

you dont call OnTriggerEnter in the update function, your game will automatically call that function when the objects overlap

avatar image Jan182 · Aug 28, 2015 at 01:07 PM 0
Share

thank you very much, now it works!!

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

27 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

Related Questions

Noob Here... Can you please give Script for moving a object into random Direction. 0 Answers

How to add a Type to a Class in Javascript? 1 Answer

OnCollisonEnter2D Not Firing after checking collider 1 Answer

Game development general question(Multiplayer - Singleplayer) 1 Answer

Running C# Function from JS file 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