- Home /
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