Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by phil2988 · Jul 25, 2013 at 07:52 PM · beginner

How do i enter a script from another scriptP (C#)

In my game I want to have a car to move when the player is close enough to the car. so I want to check the player position witch I have done. then I have to enter that value from my car to activate the animation (the animation part do I already have)

hope there is some wonderful people that can help me! :-)

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 phil2988 · Jul 30, 2013 at 08:44 PM 0
Share

Okay guys, thanks for the help. I now got it fixed XD

1 Reply

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

Answer by clunk47 · Jul 26, 2013 at 02:32 PM

Click here to be redirected to the Unity Script Reference for GetComponent

With little to go on here, like what scripts you are using and what objects those scripts are on, I'll show you a basic example of how to access a script that is attached to another object.

 //Example.cs
 //This would be an example of a script attached to your player, accessing a script on an object with Tag "Car".
 
 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour
 {
     GameObject car;
     
     void Awake()
     {
         car = GameObject.FindGameObjectWithTag("Car");
         if(car != null)
         {
             if(car.GetComponent<Animation>())
                 car.GetComponent<Animation>().Play();    
             else
                 print ("No animation attached to "+car.name);
         }
     }
 }

Get your distance like so:

 GameObject car;
 float distance;
  
 distance = Vector3.Distance(car.transform.position, transform.position);
  
 if(distance <= 14)
     car.animation.Play();

Comment
Add comment · Show 6 · 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 phil2988 · Jul 29, 2013 at 06:15 PM 0
Share

I can see that you are a lot better than I am!!! I have been scripting ½ year. do you think something like this would work too??

public GameObject Car;

if(Car.transform.position.y >= 14) { animation.Play(aimation) }

avatar image roojerry · Jul 29, 2013 at 06:30 PM 0
Share

sounds like you want something like:

 if(Vector3.Distance(Car.transform.position, Player.transform.position) <= 14) Car.animation.Play();
avatar image Jamora · Jul 29, 2013 at 06:45 PM 0
Share

There is also a video tutoral on GetComponent in the Beginner Scripting Unity Learn $$anonymous$$odule.

avatar image clunk47 · Jul 29, 2013 at 08:28 PM 1
Share

@phil2988 that just means the car's position on the y axis would need to move up before the animation plays. If you want to check the distance between your player and your car you would do something like this:

 GameObject car;
 float distance;
 
 distance = Vector3.Distance(car.transform.position, transform.position);
 
 if(distance <= 14)
     car.animation.Play();

This is just a quick example, you'd need to use your own methods of course.

avatar image phil2988 · Jul 30, 2013 at 08:25 PM 0
Share

what I want is to check when my player is at 14 on the y axe and then play the animation

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.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Multiple Cars not working 1 Answer

Unity jumping sound, help. 1 Answer

function Start () problem!! 3 Answers

Help with Basic Movement Script 1 Answer

What am I doing wrong in this 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