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 Ddres0605 · Dec 24, 2014 at 08:12 AM · spawner

Calling a variable from a spawner with multiple spawners

So my game with have 4 spawners. One for each cardinal direction. They spawn cubes they should move toawrds the center. The spawners are different in that I have a string so I can name then North, South,etc. When I spawn the cubes I need a velocity based on the string. I need the cube to know which spawner it came from. I don't know how to do this. I think I need to call the spawn script from the script thats on the spawned cube?

 using UnityEngine;
 using System.Collections;
 
 public class directionalSpawner : MonoBehaviour {
 
     public string Direction;
     public GameObject Cube;
     public Vector2 velo;
     private string key;
 
     // Use this for initialization
     void Start () {
         switch (Direction) 
         {
         case "North":
             velo = new Vector2(0.0f,-10.0f);
             key = "W";
             break;
         case "South":
             velo = new Vector2(0.0f, 10.0f);
             key = "S";
             break;
         case "East":
             velo = new Vector2(-10.0f, 0.0f);
             key = "D";
             break;
         case "West":
             velo = new Vector2(10.0f, 0.0f);
             key = "A";
             break;
         }
         StartCoroutine (Spawn ());
 
     }
     
     // Update is called once per frame
     void FixedUpdate () {
     
     }
 
     IEnumerator Spawn(){
         while (Input.GetKeyDown(key)) {
             Vector3 spawnPosition = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, 0.0f);
             Quaternion spawnRotation = Quaternion.identity;
             Instantiate (Cube, spawnPosition, spawnRotation);
             Cube.rigidbody2D.velocity = velo;
             yield return new WaitForSeconds (1.0f);
         }
         
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hav_ngs_ru · Dec 25, 2014 at 04:53 PM

The first way (you need to declare public field in script attached to cube):

 newCube.GetComponent().somePublicFieldInCubeController = key;

The sesond way (you need make a function to accept string data from anybody):

 newCube.SendMessage("SetSpawnerKey", key);  // this will call SetSpawnerKey(string key) in cube controller

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 hav_ngs_ru · Dec 25, 2014 at 04:56 PM 0
Share

and one more - you should store reference tou your cube object to interact with it! In your code your Cube is reference to prefab, not to object was created. Fix it like this:

  GameObject newCube = Instantiate (Cube, spawnPosition, spawnRotation);
  newCube.rigidbody2D.velocity = velo;
  ...

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

Zombie Spawner Not Spawn Zombies 1 Answer

Activate a script if an object is in the game world? 4 Answers

My Spawner Spawns Forever until Unity Crashes 2 Answers

Odd player teleporting 0 Answers

Enemy Spawner based on enemies values ! 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