c# - How to set different properties behind instantiated objects? -
i making game in unity c# there 10 spheres instantiated @ start of game. when user clicks on of sphere, sphere hides using set active false property. want place bombs behind of spheres, when user clicks on sphere has bomb behind it, game over.
spheres
list<gameobject> hat; float q, w, r; public spheres() { hat = new list<gameobject>(); q = -2.5f;w = -2.5f;r = -2.5f; } public void addsphere(gameobject gola) { (int = 0; < 5; i++) { gameobject abc = gameobject.instantiate (gola); abc.transform.position = new vector3 (abc.transform.position.x + q, abc.transform.position.y, abc.transform.position.z + 1.5f); hat.add (abc); q += 1.15f; } } public void hidesphere() { if(input.getmousebuttondown(0)) { ray ray = camera.main.screenpointtoray (input.mouseposition); raycasthit hit; if (physics.raycast (ray, out hit)) { if (hit.transform.gameobject.comparetag ("sphere")) { hit.transform.gameobject.setactive (false); } } } }
instantiating spheres
for (int = 0; < 5; i++) { gameobject abc = gameobject.instantiate (gola); abc.transform.position = new vector3 (abc.transform.position.x + q, abc.transform.position.y, abc.transform.position.z + 1.5f); hat.add (abc); q += 1.15f; }
since haven't add code yet, explain how approach issue. assume have created gameobject spheres has got attached script deactivate gameobeject or make not visible onclick(). steps follow be:
- add boolean variable gameobject called
boolean hasbomb
- when instantiate in scene 10 spheres, set randomly boolean true or false
- optional- if want add bomb prefabs, can instantiate them in same position of spheres hasbomb attribute set true
- in
onclick()
method of spheres check if(hasbomb) set true - in case true, load new scene called gameover text or menu. using
application.loadlevel("gameover"
)
edit code provided in comments:
list<gameobject> hat; float q, w, r; public spheres() { hat = new list<gameobject>(); q = -2.5f; w = -2.5f; r = -2.5f; } public void addsphere(gameobject gola) { (int = 0; < 5; i++) { gameobject abc = gameobject.instantiate (gola); abc.transform.position = new vector3 (abc.transform.position.x + q, abc.transform.position.y, abc.transform.position.z + 1.5f); hat.add (abc); q += 1.15f; } } public void hidesphere() { if(input.getmousebuttondown(0)) { ray ray = camera.main.screenpointtoray (input.mouseposition); raycasthit hit; if (physics.raycast (ray, out hit)) { if (hit.transform.gameobject.comparetag ("sphere")) { hit.transform.gameobject.setactive (false); } } } }
i assume gola gameobject have created in editor. instead of tagging gameobject "sphere" later detect if press on button using physics.raycast, can create script called hidesphere.cs. in script should add:
public boolean hasbomb;
and also
function onmousedown() { if(hasbomb) application.loadlevel("gameover") renderer.enabled = false; //or //destroy(gameobject); }
then should:
- add collider gola gameobject (so detects clicks)
- attach script hidesphere.cs gola gameobject
edit 2:
now randomly decide if sphere has bomb or not:
gameobject abc = gameobject.instantiate (gola); abc.transform.position = new vector3 (abc.transform.position.x + q, abc.transform.position.y, abc.transform.position.z + 1.5f); //here decide if has bomb or not float chance = random.range(0.0f, 1.0f); if(chance > 0.75f) abc.getcomponent.<hidesphere>().hasbomb = true; hat.add (abc);
note: line abc.getcomponent.<hidesphere>().hasbomb = true;
access variable hasbomb, public variable in script hidesphere.cs, attached gameobject gola. in case modify attribute particular instance generated in current iteration.
Comments
Post a Comment