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 kipa1996 · Jun 30, 2018 at 11:41 AM · c#unity 5video game

Changing one script through another?

Hello!

Im currently making my first ever video game based of the tutorial made by Brackeys on youtube. I've made a little own "spin" on it but Im currently a little stuck. I've so far been able to make the game work so blocks are spawning one at a time in fixed spawnpoints with a certain speed moving towards the "player". I've also managed to in the script for the obstacles moving add speed to them whenever the in-game time exceeds certain "checkpoints". Now I want to make it so that in the script for the obstacles moving where i've programmed so they move faster that also at the same time the interval between spawning the blocks increase aswell. My problem here is that the programing for the blocks Spawning time is in another script. I've tried googling a little bit but have not found anything useful so far. I hope this makes sense and I will add the two scripts down below.

Thanks in advance! (EDIT - forgot to mention that i use c#)

 using UnityEngine;

 public class BlockSpawner : MonoBehaviour 
 {

 public Transform[] SpawnPoints;

 public GameObject BlockPrefab;

 public float TimeBetweenSpawns = 1f;

 private float TimeToSpawn = 2f;

 

 void Update()
 {
     if (Time.time >= TimeToSpawn)
     {
         SpawnBlocks();
         TimeToSpawn = Time.time + TimeBetweenSpawns;
     }

     
 }

 void SpawnBlocks ()
 {
     int randomindex = Random.Range(0, SpawnPoints.Length);

     for (int i = 0; i < SpawnPoints.Length; i++)
     {
         if (randomindex == i)
         {
             Instantiate(BlockPrefab, SpawnPoints[i].position, Quaternion.identity);
         }
     }



 }
 
 
 
 }

(Break for easier viewing)

 using UnityEngine;

 public class ObstacleMovement : MonoBehaviour {

 public Rigidbody rb;

 public float ForwardForce = -2000f;

 void FixedUpdate ()

     
 {
     
     rb.AddForce(0, 0, ForwardForce * Time.deltaTime);

     if (Time.time > 5)
     {
         ForwardForce = -6000f;
         
     }

     if (Time.time > 10)
     {
         ForwardForce = -7000f;
     }

     if (Time.time > 20)
     {
         ForwardForce = -8000f;
     }

   }
 }
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

Answer by madks13 · Jun 30, 2018 at 11:50 AM

When you instantiate your BlocPrefab, put it in an array. Either get the script as component from the block or from the array, or put only the scripts in the array : List blocks = new List();

 var go = GameObject.Instantiate(blockPRefab);
 blocks.Add(go);
 var om = go.GetComponent<ObstacleMovement>();
 //Do something with obstacle

or : List obstacles = new List();

 obstacles.Add(GameObject.Instantiate(blockPrefab).GetComponent<ObstacleMovement));
 
 //Do something with obstacles
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 Ermiq · Jun 30, 2018 at 02:06 PM

If BlockSpawner script is designed to have the only one instance at runtime (so it's not that every block uses its own BlockSpawner script instance but all blocks use the same instance of BlockSpawner) then you could make a static instance of the script to manage its variables from another classes:

  using UnityEngine;
  public class BlockSpawner : MonoBehaviour 
  {
     public static BlockSpawner Instance; //this gonna be yout access point
          // to all variables in this class
     
     public Transform[] SpawnPoints;
     public GameObject BlockPrefab;
     public float TimeBetweenSpawns = 1f;
     public float TimeToSpawn = 2f;

     void Awake ()
     {
          // Initialize it here (we make Instance variable to store this class instance)
          Instance = this;
     }
     
     void Update()
     {
          if (Time.time >= TimeToSpawn)
         {
              SpawnBlocks();
              TimeToSpawn = Time.time + TimeBetweenSpawns;
          }
      }
  }

Now you can get access to BlockSpawner variables from another class by using

 BlockSpawner.Instance._public_variable_in_blockSpawner_script

Like this:

 using UnityEngine;
 public class ObstacleMovement : MonoBehaviour
 {
      public Rigidbody rb;
      public float ForwardForce = -2000f;

      void FixedUpdate ()
     {
           rb.AddForce(0, 0, ForwardForce * Time.deltaTime);
           if (Time.time > 5)
           {
                ForwardForce = -6000f;

                 BlockSpawner.Instance.TimeBetweenSpawns = 0.5f;
                 BlockSpawner.Instance.TimeToSpawn = 1f;
            }
      }
  }

You also can make separate special class to store and manage all variables you could use. For example, I have a PlayerCharacter class and I manage all player's states there (such as bIsPlayerRunning, bIsPlayerShooting) and then I check these variables in other classes and do things like running animations and so on, and I can make PlayerCharacter.bIsPlayerShooting=false in the animations class when he's running and I don't need to do it in PlayerCharacter class.

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

174 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity3d and monodevelop c# scripting on ubuntu 16.04 IntelliSense problems 0 Answers

Object Reference Not Set To An Instance of An Object Error 1 Answer

how to control scale 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