# Referenced script only working for one asset Unity C#

In my game, there is a maze, which has its walls generated by several types of points. In the top row, there are points that I call "type A" point - they only generate walls vertically (downward). "Type B" points have only horizontal (sideways) walls, and "type C" points generate both horizontal and vertical walls. All "A, B and C" points also generate items

because of this, I am using "shared" functions that are referenced on another script, in order to boost efficiency and so that I don't have identical code for the functions of generation on these points, as they all generate items the same way, and where applicable, generate horizontal and vertical walls in exactly the same way.

The issue here is, that I have a central script, that, within the scripts for "A, B and C", is referenced (public void function). the referencing works perfectly well.

however all the vertical walls generate on exactly the same point "A", (of which is first in terms of it's position in the row, and additionally first in the hierarchy); instead of all the different point "A"'s, meaning the rest of the point "A"'s are blank.

My function is "GenVwall", short for "Generate Vertical Wall", located in the central script:

public void GenVwall () {

GameObject objV;

//Decide the probability of there being a tile
float lvlF = Mathf.Log10(gameManagerS.lvlNum);
float pointF = 38.2f + (9.8f * (lvlF + 1));
float decideV = Random.Range(0, 100);

//Spawn different wall types (vertically)
if (decideV < pointF) {
int vWall = Random.Range (0, 40);
if (vWall <= 24) {
objV = Instantiate (Resources.Load ("tiles/wallReg"), new Vector2 (transform.position.x, (transform.position.y - 0.417f)), transform.rotation) as GameObject;
} else if (vWall > 24 && vWall <= 26) {
objV = Instantiate (Resources.Load ("tiles/wallSpiked"), new Vector2 (transform.position.x, (transform.position.y - 0.417f)), transform.rotation) as GameObject;
} else if (vWall > 26 && vWall <= 31) {
objV = Instantiate (Resources.Load ("tiles/wallBreak"), new Vector2 (transform.position.x, (transform.position.y - 0.417f)), transform.rotation) as GameObject;
} else if (vWall > 31 && vWall <= 35) {
objV = Instantiate (Resources.Load ("tiles/wallHalf"), new Vector2 (transform.position.x, (transform.position.y - 0.417f)), transform.rotation) as GameObject;
} else if (vWall > 35 && vWall <= 38) {
objV = Instantiate (Resources.Load ("tiles/wallGate"), new Vector2 (transform.position.x, (transform.position.y - 0.417f)), transform.rotation) as GameObject;
} else if (vWall > 38) {
objV = Instantiate (Resources.Load ("tiles/wallExplode"), new Vector2 (transform.position.x, (transform.position.y - 0.417f)), transform.rotation) as GameObject;
}
}
}


and the referencing of the public void function, located within the "A" point script, is:

GameObject tempObj = GameObject.Find("genPointA");
genPointS = tempObj.GetComponent<GenPointS>();
genPointS.GenVwall();


Now, I believe the issue lies within the fact i use transform.position, and it's referring to the position of the first "point A" in the hierarchy.

Now, I'd like to know,

Is there a better way to make use of these functions? as I can't consider it too efficient to have the same function copy-pasted into the individual scripts for point "A, B and C".

A few strategies you can use:

1. Don't use different scripts for A, B, and C. Just make one wall-generator script where you can control horizontal or vertical wall spawning in data. eg...

public class WallSpawner : MonoBehaviour {
public bool vertical; // Enable for A & C instances
public bool horizontal; // Enable for B & C instances

public void Spawn() {
if(vertical) GenerateVerticalWall();
if(horizontal) GenerateHorizontalWall();
}

}
2. Have A, B, and C derive from a common WallSpawner class, and define their shared methods there (in typical OOP inheritance style)

3. If your shared wall-spawning logic isn't going to be attached to the instance it's spawning from anyway, then why not make it static? No messy Find/GetComponent required!

public static GenerateVerticalWallAt(Transform location) {
// Use the location argument for positioning and orienting the wall.
...
}

Then your instance call this and pass its own transform as the spawn location.

SharedSpawningLogicClass.GenerateVerticalWallAt(this.transform);

• This worked perfectly fine, and i shall now use this approach for the remainder of my project - it is not only quicker to program, but it appears more efficient, and, I find, allows less room for errors (as there are less scripts) many, many thanks – AntonyBoys Apr 12 '18 at 11:36
• however, I instead modified it a bit, and, while the script is attached to all my type "A, B and C" walls, different functions occur based on the item's tag (I tag them in the inspector as "A, B, C" and so on) – AntonyBoys Apr 12 '18 at 11:37