Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
0
Question by Sectah · May 25, 2016 at 05:39 PM · instantiatestaticinstantiate prefabinstantiationstatic variable

Change value of a static variable multiple times in one script

I'm spawning a number of different sized enemies that move at different speeds. I control the speed with a variable called speed in another script.

I'm trying to instantiate multiple different version of said enemy with different speeds. As seen below:

level1GM.cs

 using UnityEngine;
 using System.Collections;
 
 public class level1GM : MonoBehaviour {
 
     public GameObject enemy;
     Vector3 size9, size17;
 
     void Start () {
         size9 = new Vector3 (0.0f, 0.0f, 8.0f);
         size17 = new Vector3 (0.0f, 0.0f, 16.0f);
 
         enemyMovement.speed = 6f;
         enemy.transform.localScale += size9;
         Instantiate (enemy, new Vector3(-20.0f, 0.0f, 5.5f), Quaternion.identity);
         enemy.transform.localScale -= size9;
 
         enemy.transform.localScale += size9;
         Instantiate (enemy, new Vector3(20.0f, 0.0f, -5.5f), Quaternion.Euler(0.0f, 180.0f, 0.0f));
         enemy.transform.localScale -= size9;
 
         enemyMovement.speed = 4f;
         Instantiate(enemy, new Vector3(9.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(2.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(-1.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(-3.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(-5.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(-7.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(-9.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
 
         enemyMovement.speed = 4.2f;
         Instantiate(enemy, new Vector3(-9.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(-2.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(1.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(3.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(5.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(7.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(9.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
 
         enemyMovement.speed = 7f;
         enemy.transform.localScale += size17;
         Instantiate (enemy, new Vector3(-1.5f, 0.0f, 37.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         enemy.transform.localScale -= size17;
     }
 }

enemyMovement.cs

 using UnityEngine;
 using System.Collections;
 
 public class enemyMovement : MonoBehaviour {
 
     public static float speed = 1f;
 
     // Use this for initialization
     void Start () {
         Destroy (gameObject, 15f);
     }
     
     // Update is called once per frame
     void Update () {
         transform.Translate (speed * Time.deltaTime, 0.0f, 0.0f);
     }
 
     void OnCollisionEnter (Collision col){
         if (col.collider.tag == "Player") {
             Debug.Log ("Death");
         }
     }
 }
 

But it will only use the last enemyMovement.speed because of course it's in start. But how would I go about having different speeds of different instantiated objects? Would I need a variety of different prefabs?

Thank you.

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

2 Replies

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

Answer by Sectah · May 25, 2016 at 06:31 PM

Managed to solve it with help from @tanoshimi

Got rid of static from the other script then used GetCompenent instead for the following:

 using UnityEngine;
 using System.Collections;
 
 public class level1GM : MonoBehaviour {
 
     public GameObject enemy;
     Vector3 size9, size17;
     private enemyMovement enemyMovementScript;
 
     void Start () {
         size9 = new Vector3 (0.0f, 0.0f, 8.0f);
         size17 = new Vector3 (0.0f, 0.0f, 16.0f);
 
         enemyMovementScript = enemy.GetComponent<enemyMovement>();
 
         enemyMovementScript.speed = 6f;
         enemy.transform.localScale += size9;
         Instantiate (enemy, new Vector3(-20.0f, 0.0f, 5.5f), Quaternion.identity);
         enemy.transform.localScale -= size9;
 
         enemy.transform.localScale += size9;
         Instantiate (enemy, new Vector3(20.0f, 0.0f, -5.5f), Quaternion.Euler(0.0f, 180.0f, 0.0f));
         enemy.transform.localScale -= size9;
 
         enemyMovementScript.speed = 4f;
         Instantiate(enemy, new Vector3(9.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(2.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(-1.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(-3.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(-5.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(-7.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(-9.5f, 0.0f, 25.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
 
         enemyMovementScript.speed = 4.2f;
         Instantiate(enemy, new Vector3(-9.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(-2.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(1.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(3.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(5.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(7.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         Instantiate(enemy, new Vector3(9.5f, 0.0f, 32.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
 
         enemyMovementScript.speed = 7f;
         enemy.transform.localScale += size17;
         Instantiate (enemy, new Vector3(-1.5f, 0.0f, 37.0f), Quaternion.Euler(0.0f, 90.0f, 0.0f));
         enemy.transform.localScale -= size17;
     }
 }

Thanks for the help. :)

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
1

Answer by tanoshimi · May 25, 2016 at 05:44 PM

It's nothing to do with it being set in Start(). It's because you've declared speed as static. Why did you do this if you want different enemies to have different speeds?

Comment
Add comment · Show 1 · 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 Sectah · May 25, 2016 at 06:03 PM 0
Share

It doesn't let me access enemy$$anonymous$$ovement.speed unless the variable is static. I get this error:

 error CS0120: An object reference is required to access non-static member `enemy$$anonymous$$ovement.speed'

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Assign variables on Instantiation? 0 Answers

Infinite Instantiation 2 Answers

How to Change the Variables of an Instantiated Object? 0 Answers

Instantiated object not in right position 1 Answer

Instantiation Problem 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