• 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 /
avatar image
0
Question by Crashade · May 07, 2014 at 05:52 PM · 2djavascriptcollisionvariablenoob

How do you change a variable in a script, that is on an object instantiated from a prefab?

I'm making a 2d game where the player drives down three lanes at an increasing speed while dodging other cars. When you crash in to another car I want both cars to stop. My problem is that if I just set the cars speed to 0, all the cars stop.

Here is the script on the player:

 var godMode = false;
 var deadCar : Animator;
 static var carCollision = false;
 deadCar = GetComponent(Animator);
 
 
 function OnTriggerEnter2D(other: Collider2D) {
     if (!godMode) {
         //The cars collide
         forwardmover.speed = 0;
         deadCar.SetTrigger("Dead");
         carCollision = true;
     };
 }

Here is the script on the other cars:

 static var carSpeed : float = 5;
 
 function Update () {
     transform.Translate(transform.up * carSpeed * Time.deltaTime);
 }

As I said I want to change the "carSpeed" variable to 0 on the car, I collide with. Any help is very much apreciated

Comment
Add comment · Show 4
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 AlucardJay · May 07, 2014 at 05:55 PM 1
Share

static variables make my eyeballs bleed. Don't use static variables, it is the lazy way out of learning to use GetComponent and caching references to components. public variables and GetComponent is the answer to your question.

avatar image Crashade · May 07, 2014 at 06:03 PM 0
Share

static variables make my eyeballs bleed. Don't use static variables, it is the lazy way out of learning to use GetComponent and caching references to components. public variables and GetComponent is the answer to your question.

Thanks for the answer. I don't know how public variables and GetComponent work. Can you tell me how I would implement them here?

avatar image Lo0NuhtiK · May 07, 2014 at 06:11 PM 1
Share

https://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html

http://unity3d.com/learn/tutorials/modules/beginner/scripting/variables-and-functions

http://msdn.microsoft.com/en-us/library/aa691160%28v=vs.71%29.aspx

http://www.w3schools.com/js/js_variables.asp

avatar image robertbu · May 07, 2014 at 08:34 PM 0
Share

http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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

Related Questions

How to make it so enemies only move towards the player when the player is colliding with an object 1 Answer

Variable in String 1 Answer

Player Destroy on Collision? 0 Answers

Unity 2D Colliders not Colliding 2 Answers

Access a JavaScript variable from a C# script? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges