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 /
This question was closed Aug 28, 2015 at 04:57 PM by FermentedCorn for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by FermentedCorn · Aug 28, 2015 at 01:02 PM · c#speed

How can I adjust the speed of an object through a seperate script?

Hello,

I have an assignment where I have to apply an enemy script (provided by my teacher) to an object, what this script does is tell the enemy where to walk and how fast to walk. I am tasked to slow down the enemy when it collides with a trigger but I can not modify the Enemy.cs script in any way.

Here is the script:

 using UnityEngine;
 using System.Collections;
 
 // If you guys need any other function please let me know - Kevin
 public class Enemy : MonoBehaviour 
 {    
     private int health = 100;
     private float speed = 5f;
     public Transform[] path = null;
     private int pathIndex = 0;
     private float minDistance = 0.2f;
     private int damage = 1;
     public string targetTag = "";
         
     void Update () 
     {
         MoveInPath();
     }
 
     private void MoveInPath()
     {
         if (path.Length > pathIndex)    // "As long as the array is big enough..." - Avoids an index out of range exception
         {
             if (path[pathIndex] != null)    // "... and the current element exists"    - Avoids Null Reference exceptions
             {
                 transform.position += (path[pathIndex].position - transform.position).normalized * speed * Time.deltaTime; // Move towards the current target
                 if (Vector3.Distance(transform.position, path[pathIndex].position) < minDistance)    // "If I'm close enough to the target, select the next target"
                 {
                     pathIndex++;
                 }
             }
         }
     }
 
     private void die()
     {
         Destroy(this.gameObject);
     }
 
     public void takeDamage(int damage)
     {
         health -= damage;
         if (health <= 0)
         {
             die();
         }
     }    
     
     public void setSpeed (float newSpeed)
     {
         speed = newSpeed;
     }
 }


I've tried editing the speed variable in another script but it just doesn't work, it seems as though that the speed variable doesn't actually affect the speed at which the enemy walks.

Please leave your suggestions if you have any.

Thanks!

,

Comment
Add comment · Show 3
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 Dave-Carlile · Aug 28, 2015 at 01:03 PM 0
Share

Wouldn't you just call the provided setSpeed method?

avatar image FermentedCorn · Aug 28, 2015 at 01:23 PM 0
Share

Thanks for responding.

I did but it didn't actually change the speed of the enemy, also it is set as private so I am not allowed to modify it in any way as my $$anonymous$$cher instructed.

avatar image Dave-Carlile · Aug 28, 2015 at 03:46 PM 0
Share

The setSpeed function is public, not private.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Duugu · Aug 28, 2015 at 03:46 PM

You're probably mixing up things.

The speed variable itself is private. You can't access it.

 private float speed = 5f;

The person who wrote this script was providing a public function to modify the variable:

 public void setSpeed (float newSpeed)

So, please use the setSpeed function. This is what the function is for.

You'll have to get a reference to the gameobject the script is assigned to. (GetGameobject etc.) Then get a reference to the script component of the gameobject. (GetComponent() etc.) Then use the reference to call setSpeed.

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 FermentedCorn · Aug 28, 2015 at 04:16 PM 0
Share

$$anonymous$$aybe I am doing it wrong? To test to see if that actually changed the speed of the enemy I just wrote in the Enemy script setSpeed(newSpeed = 1f);

Is that correct?

When I tried that it didn't work, it was walking the same speed.

Thanks.

avatar image FermentedCorn · Aug 28, 2015 at 04:56 PM 0
Share

I was doing it wrong. I got it working now. Thanks all :)

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I slow down a quickly moving spaceship 0 Answers

My bullet won't go forward? Please check my script and advise me 2 Answers

Can anyone explain what an offset is? 0 Answers

Unity SpeedPad / boost pad 1 Answer

LookRotation with constant speed 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