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 phoda · Jun 08, 2015 at 10:12 AM · functionboolean

Question about calling functions from other scripts.

Okay I really dont how to really define this question or explain but i will give it a try.

When i from ScriptA call public function of ScriptB to instantiate gameobject it does not save it to variable only when i call function from ScriptB

I resolved problem to set boolean of scripta to true and in scriptb when its true it calls function but iam wondering why doesnt calling function from scripta doesnt work?

EDIT: here is whole code

 using UnityEngine;
 using System.Collections;
 
 public class Spawner : MonoBehaviour {
 
     //Store Blocks, Turret Blocks, Next Blocks and Ghost Blocks
     public GameObject[] blocks;
     public GameObject[] row;
     public GameObject[] display;
     public GameObject[] ghost;
 
     //Store next 4 blocks and 3 blocks of row
     public float[] next = new float[4];
     public static float[] rows = new float[3];
 
     //Stores next blocks
     GameObject obj1;
     GameObject obj2;
     GameObject obj3;
     GameObject obj4;
 
     //Stores blocks to shoot in turret mode
     GameObject r1;
     GameObject r2;
     GameObject r3;
 
     //Stores ghost object
     GameObject gt;
 
     //For counting how many rows have been cleared
     private int count = 0;
     //Should row be checked for 
     public static bool checkrows = false;
     //Bool to spawn next row
     bool spawnrow = false;
     //Time to resume game but x2
     float resume = 1f;
 
     //Set next 4 blocks on
     void Awake()
     {
         next[0] = Random.Range(0, blocks.Length);
         next[1] = Random.Range(0, blocks.Length);
         next[2] = Random.Range(0, blocks.Length);
         next[3] = Random.Range(0, blocks.Length);
     }
     
     // Update is called once per frame
     void Update () {
 
         if (GM.spawnnext == true)
         {
             SpawnNext();
             GM.spawnnext = false;
         }
 
         if (GM.spawnrow == true)
         {
             SpawnRow();
             GM.spawnrow = false;
         }
 
         if (checkrows == true)
         {
             if (rows[0] != 8 && rows[1] == 8 && rows[2] == 8)
             {
                 next[count] = rows[0];
                 count++;
                 Destroy(r1);
                 spawnrow = true;
             }
 
             if (rows[0] == 8 && rows[1] != 8 && rows[2] == 8)
             {
                 next[count] = rows[1];
                 count++;
                 Destroy(r2);
                 spawnrow = true;
             }
 
             if (rows[0] == 8 && rows[1] == 8 && rows[2] != 8)
             {
                 next[count] = rows[2];
                 count++;
                 Destroy(r3);
                 spawnrow = true;
             }
 
             if (rows[0] == 8 && rows[1] == 8 && rows[2] == 8)
             {
                 next[count] = Random.Range(0, blocks.Length);
                 count++;
                 Destroy(r1);
                 Destroy(r2);
                 Destroy(r3);
                 spawnrow = true;
             }
 
             if (count > 3)
             {
                 Invoke("TMode", resume);
                 transform.position = new Vector3(5, 20, 0);
                 count = 0;
                 spawnrow = false;
                 Destroy(r1);
                 Destroy(r2);
                 Destroy(r3);
 
             }else if (spawnrow == true){
                 SpawnRow();
                 spawnrow = false;
             }
 
             checkrows = false;
 
         }
     
     }
 
     //Spawns next block
     public void SpawnNext()
     {
         if (GM.tmode == false)
         {
             Destroy(gt);
             transform.position = new Vector3(5, 20, 0);
             Instantiate(blocks[(int)next[0]], transform.position, Quaternion.identity);
             gt = Instantiate(ghost[(int)next[0]], transform.position, Quaternion.identity) as GameObject;
 
             //Updates next blocks
             next[0] = (int)next[1];
             next[1] = (int)next[2];
             next[2] = (int)next[3];
             next[3] = Random.Range(0, blocks.Length);
 
             UpdatePicture();
         }
     }
 
     //Spawns next row
     public void SpawnRow()
     {
 
         if (GM.tmode == true)
         {
             transform.position = new Vector3(47, 20, 0);
             int i1 = Random.Range(0, row.Length);
             r1 = Instantiate(row[i1], transform.position, Quaternion.identity) as GameObject;
             rows[0] = i1;
 
             transform.position = new Vector3(50, 20, 0);
             int i2 = Random.Range(0, row.Length);
             r2 = Instantiate(row[i2], transform.position, Quaternion.identity) as GameObject;
             rows[1] = i2;
 
             transform.position = new Vector3(53, 20, 0);
             int i3 = Random.Range(0, row.Length);
             r3 = Instantiate(row[i3], transform.position, Quaternion.identity) as GameObject;
             rows[2] = i3;
         }
 
     }
 
     //Updates picture of next blocks
     void UpdatePicture()
     {
         //Destroys previous next blocks
         Destroy(obj1);
         Destroy(obj2);
         Destroy(obj3);
         Destroy(obj4);
 
         //Creates new next blocks
         obj1 = Instantiate(display[(int)next[0]], new Vector3(11, 9, 0), Quaternion.identity) as GameObject;
         obj2 = Instantiate(display[(int)next[1]], new Vector3(11, 12, 0), Quaternion.identity) as GameObject;
         obj3 = Instantiate(display[(int)next[2]], new Vector3(11, 15, 0), Quaternion.identity) as GameObject;
         obj4 = Instantiate(display[(int)next[3]], new Vector3(11, 18, 0), Quaternion.identity) as GameObject;
     }
 
     void TMode()
     {
         Cam.moda = true;
         Invoke("Resume", resume);
     }
 
     void Resume()
     {
         GM.tmode = false;
         SpawnNext();
     }
 }


 using UnityEngine;
 using System.Collections;
 
 public class GM : MonoBehaviour {
 
     //Creates grid with set width and height
     public static int width = 10;
     public static int height = 25;
     public static Transform[,] grid = new Transform[width, height];
 
     //Bool for Turret mode
     public static bool tmode = false;
 
     //Booleans for ghost where to move and vector 3 of block position
     public static bool gright = false;
     public static bool gleft = false;
     public static bool grotate = false;
     public static bool gupdate = false;
     public static Vector3 blockpos;
 
     //Boolean if turret needs to be turned in right direction
     public static bool rotateturret = false;
 
     //Reference to spawner Spawner script
     public static bool spawnnext = false;
     public static bool spawnrow = false;
     public Spawner spawner;
 
     // Use this for initialization
     void Start () {
         spawner.SpawnNext();
     }
 
     public bool isValidPos(Vector3 v, Transform transform)
     {
         if (v.x < 0)
             return false;
 
         if (v.x >= width)
             return false;
 
         if (v.y < 0)
             return false;
 
         if (grid[(int)v.x, (int)v.y] != null)
             if (grid[(int)v.x, (int)v.y].parent != transform)
                 return false;
 
         return true;
     }
 
     //Check all rows and delete first full
     public void DeleteFull()
     {
 
         int cleared = 0;
 
         //Checks how many rows it has cleared and goes to turret mode if 4 are cleared
         for (int y = 0; y < height; y++)
         {
             if (isRowFull(y) == true)
             {
                 cleared++;
                 if (cleared == 4)
                 {
                     Combo();
                 }
             }
         }
 
         //Delete full row
         for (int y = 0; y < height; y++)
         {
             if (isRowFull(y) == true)
             {
                 DeleteRow(y);
             }
         }
     }
 
     //Return true if row full
     bool isRowFull(int y)
     {
         for (int x = 0; x < width; x++)
         {
             if (grid[x, y] == null)
             {
                 return false;
             }
         }
         return true;
     }
 
     //Deletes y row
     void DeleteRow(int y)
     {
         for (int x = 0; x < width; x++)
         {
             Destroy(grid[x, y].gameObject);
             grid[x, y] = null;
         }
 
         for (int r = y + 1; r < height; r++)
         {
             for (int x = 0; x < width; x++)
             {
                 if (grid[x, r] != null)
                 {
                     grid[x, r].position += new Vector3(0, -1, 0);
                     grid[x, r - 1] = grid[x, r];
                     grid[x, r] = null;
                 }
             }
         }
 
         //Check again if there is full rows and if yes then delete
         DeleteFull();
     }
 
     //Move all rows above deleted one
     void MoveAbove(int y)
     {
         for (int r = y; r < height; r++)
             MoveDown(r);
     }
 
     //Move all blocks in row down
     void MoveDown(int y)
     {
         for (int x = 0; x < width; x++)
         {
             if (grid[x, y] != null)
             {
                 grid[x, y].position = new Vector3(0, -1, 0);
                 grid[x, y - 1] = grid[x, y];
                 grid[x, y] = null;
             }
         }
     }
 
     void Combo()
     {
         rotateturret = true;
         tmode = true;
         Invoke("TMode", 2f);
     }
 
     void TMode()
     {
         Cam.modb = true;
         Invoke("SpawnRows", 2f);
     }
 
     void SpawnRows()
     {
         spawnrow = true;
     }
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.A))
             Combo();
     }
 }



In GM script iam calling 2 Spawner functions SpawnNext and SpawnRow. from GM i must call SpawnNext with reference call and SpawnRow i have to call with boolean

(FOR EASIER FINDING i call them in GM at START and in COMBO function at END)

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Check a boolean function without using Update() 1 Answer

Waiting for a Function to Complete 1 Answer

Better way to delay a function for a few seconds? Javascript 1 Answer

How to detect if a function is "happening"? 1 Answer

Animation Event Bool Function? 1 Answer


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