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 Jab2565 · Aug 17, 2013 at 08:02 AM · getcomponent

Accessing other scripts using GetComponent()

I've been working on my runner game and I'm having some difficulty making using of GetComponent. What I want to do is have a script: GameController store an enemy speed variable that every second, I want it raise the global speed of my enemy objects.

My problem is that I'm having trouble figuring out how to use GetComponent to get it to work. Is there a way to access the script: EnemyBehavior directly and change the enemySpeed variable in it? Or do I need to do something else to modify the behavior?

What I did that didn't work was the following:

 public var lSpawnLoc: GameObject;
 public var fSpawnLoc: GameObject;
 public var startSpeed: float = 0.8;
 private var spawnObject1 : SpawnObject;
 private var spawnObject2 : SpawnObject;
 private var enemyBehavior : EnemyBehavior;
 private var newEnemySpeed : float;
 
 function Start ()
 {
 spawnObject1 = lSpawnLoc.GetComponent(SpawnObject);
 spawnObject2 = fSpawnLoc.GetComponent(SpawnObject);
 InvokeRepeating("Spawn",3.0,3.0);
 
 enemyBehavior.GetComponent(EnemyBehavior);
 
 InvokeRepeating ("AddSpeed",1.0,1.0);
 }
 
 
 
 function Spawn()
 {
  var choice: int = Random.Range(0,2);
  
  if (choice == 0)
      {
      spawnObject1.CreateEnemy();        
      }
     else
     {
     spawnObject2.CreateEnemy();     
     }
 }
 
 function AddSpeed()
 {
 enemyBehavior.enemySpeed = newEnemySpeed;
 newEnemySpeed += .01;
 }
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 whydoidoit · Aug 17, 2013 at 08:18 AM 2
Share

On my phone so can't reformat your code (highlight it and press the code 101 button). Check out the GetComponent tutorials on http://unitygems.com

avatar image Jamora · Aug 17, 2013 at 09:49 AM 0
Share

The Unity Learning $$anonymous$$odules also have tutorials on beginner Unity scripting, including a GetComponent tutorial.

I reformatted the code.

avatar image Jab2565 · Aug 18, 2013 at 08:19 PM 0
Share

I have another question about getcomponent, when using it on a gameobject, do I need to have a public variable that is assigned the other game object or script that I want to access or not?

avatar image Linus · Aug 18, 2013 at 08:25 PM 0
Share

The variables, classes and functions you want to access from another script needs to be public.

Converted your answer to comment. Please only post answers if they are answers.

If you feel this question is answered, tick the box next to the answer you think is the best and close this question.

Also have a look at the FAQ http://answers.unity3d.com/page/faq.html

1 Reply

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

Answer by CVogel · Aug 17, 2013 at 09:50 AM

Let's say you have a GameController Script with an 'enemySpeedIncrease' variable defined.

GameController -> enemySpeedIncrease.

Now, from other scripts in the same scene, you can access this GameController Script as follows. Let's say you have an Enemy object with an EnemyScript script. In EnemyScript...

EnemyScript (C# example)

 using UnityEngine;
 using System.Collections;
 
 public class EnemyScript : MonoBehaviour {
     
     public float currentSpeed;
     public GameController GMC;
     
     // Use this for initialization
     void Start () {
         GMC = FindObjectOfType(typeof(GameController)) as GameController;
     }
     
     // Update is called once per frame
     void Update () {
         currentSpeed += GMC.enemySpeedIncrease;
         Debug.Log ("Enemy Speed is: " + currentSpeed);
     }
 }


You see you have to give your 2nd script a reference to the first script, and load it with FindObjectOfType or a similar function.

Hopefully this helps!

Chris Vogel

Comment
Add comment · Show 2 · 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 fafase · Aug 17, 2013 at 10:05 AM 0
Share

Using FindObjectOfType is not appropriate. What if you have two of the type, the method will return the first found. What if you were looking for the other?

The appropriate way is GetComponent.

avatar image Jab2565 · Aug 18, 2013 at 06:04 AM 0
Share

So on the EnemyBehavior script, I would set a variable to type GameController . And say something along the lines of enemySpeed= variable.GetComponent(newEnemySpeed) ?

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

splitting a script up into multiple scripts... 1 Answer

GetComponent doesn't work anymore 2 Answers

Get the child gameObject of a parent and not the Transform 4 Answers

Can't understand generic getComponent for Js 1 Answer

Changing TextMesh text via script 2 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