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 /
avatar image
0
Question by GaventGames · Feb 27, 2019 at 05:43 PM · scripting probleminstantiatereferencefloat

How can I change a float value of an instantiated object from another script?

I have a Spawn Manager script that is in charge of spawning waves of enemies. When a wave is completed, it is told to increase the rate at which it spawns enemies:

 void WaveCompleted()
     {
         Debug.Log ("Wave Completed!");
 
         state = SpawnState.COUNTING;
         waveCountdown = timeBetweenWaves;
             nextWave++;
         newSpawnRate = spawnRate + .1f;
         spawnRate = newSpawnRate;

The script I have for the instantiated enemies is pretty simple:

 public float MoveSpeed = -2f;

 // Use this for initialization
 void Start () {
 
 }

 // Update is called once per frame
 void FixedUpdate () {
     transform.Translate (new Vector3 (MoveSpeed, 0, 0)* Time.deltaTime);
 }

 void OnCollisionEnter(Collision col){
     if (col.gameObject.tag == "Bullet")
         Destroy (col.gameObject);
 }        

Since the Spawn Manager is already detecting when we've reached a new wave, I'd like to change the MoveSpeed float from this script similar to the way it effects the spawn rate. When I try to reference it, it seems to break the speed and the enemies don't move across the screen at all and start spawning on top of each other. I don't know if I'm going about referencing the value correctly as I'm just starting coding. Any help would be greatly appreciated.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by naderlabbad309 · Feb 27, 2019 at 05:51 PM

go to your script 1 and put this this to call you script 2 and float dont forget put name yourscript 2 time

 public yourscript yourscript ;

this how call value

 yourscript .yourscript = 1

 

Comment
Add comment · 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
0

Answer by WarmedxMints · Feb 27, 2019 at 05:52 PM

Simply get the script and modify it when you spawn them.

 var go = Instantiate(EnemyPrefab);
 
 var enemyScript = go.GetComponent<MyEnemyClass>();
 
 enemyScript.MoveSpeed = 5f;
Comment
Add comment · 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
0

Answer by TheOnlyMike675 · Feb 27, 2019 at 06:55 PM

Hi there! If I understand you correctly, you may need to loop through the existing enemy objects and change the MoveSpeed variable that way.

 GameObject[] enemies;

 enemies = GameObject.FindGameObjectsWithTag("Enemy");
 foreach(GameObject enemy in enemies)
 {
     enemy.GetComponent<nameOfScriptOnEnemy>().MoveSpeed = newValue;
 }

This would only affect the speed of the already existing enemy objects I believe. In this case it may be easier to use global variables. Create an Empty GameObject, call it globals, and place the following code inside the monobehaviour of a script (call the script "globals") and add it to the globals object.

     public static globals instance = null;
     public static float MoveSpeed;
     
     
     void Awake()
         {
             if (instance == null)
             {
                 instance = this;
             }
             else
             {
                 if (instance != this)
                 {
                     Destroy(gameObject);
                 }
             }
             DontDestroyOnLoad(gameObject); //Persists between scenes
         }
 
     void Start()
     {
         MoveSpeed = -2f;
     }

The code in the Awake method is called the singleton pattern and it makes it so that there is only one of the globals object at a given time.

Then when you want to access and/or change MoveSpeed, you call it with globals.MoveSpeed in a script.

Try to avoid using globals for everything, but in some situations they're very useful. I'm also slightly new to C# so we're in this together!

I hope that helps!

Comment
Add comment · 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

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

195 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Retain references to child gameobjects while duplicating objects 0 Answers

Destroy and rebuild game objects from lists 1 Answer

One Script Referred by Another Script used by Multiple Objects 0 Answers

Object reference not set to an instance of an object (Scripts calling objects which only exist in certain scenes) 1 Answer

Referencing instantiated objects at runtime 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