Calling Transform component of an object in another class C# -




i working on tutorial , "side objectives" don't walk through try , feel it.

so, way things work @ time there player object. player object has player script.

public class player : monobehaviour {  private animator anim;//reference animator component private rigidbody rigidbody;//reference component rigidbody private audiosource audiosource;  [serializefield] private float force = 100f; [serializefield] private audioclip sfxjump; [serializefield] private audioclip sfxdeath;   }  void awake() {//these assertions ensure when writing cocde wont miss them. use team work.      assert.isnotnull (sfxjump);     assert.isnotnull (sfxdeath);  }  private bool jump = false; //check jump  // use initialization void start () {//all these getting components @ start update them code goes onwards.      anim = getcomponent<animator> ();     rigidbody = getcomponent<rigidbody> ();     audiosource = getcomponent<audiosource> ();     positionstart = getcomponent<transform> ();  }  // update called once per frame void update () {     if (!gamemanager.instance.gameover && gamemanager.instance.gamestarted) {         if (input.getmousebuttondown (0)) {//if press mouse key             gamemanager.instance.playerstarted ();             rigidbody.usegravity = true;//turn gravity on component goes down.             audiosource.playoneshot (sfxjump);             anim.play ("jump");//play animation jump             jump = true;         }     } } //fixed update physics void fixedupdate() {//use physics due frame rate. time.deltatime wont cut it.     if (jump == true) {//if jumping, turn jump off.         jump = false;         rigidbody.velocity = new vector2 (0, 0);//turn velocity 0 speed doesnt increase while falling         rigidbody.addforce (new vector2 (0, force), forcemode.impulse);//give impulse upwards.     }      //print (rigidbody.velocity.y);//print velocity. turn shit off. } //code create collision obstacles , die , fall through floor. void oncollisionenter (collision collision) {//call collision component     if (collision.gameobject.tag == "obstacle") {//if slap tagged object called obstacle         rigidbody.addforce (new vector2 (-50, 20), forcemode.impulse);//add force push cause ded         rigidbody.detectcollisions = false;//turn off ability detect collisions         audiosource.playoneshot (sfxdeath);//play ded noise         gamemanager.instance.playercollided ();         gamemanager.instance.restart ();      }     }     } 

the game manager, of course exists in camera control states of game.

public static gamemanager instance = null;//only 1 in memory. 1 gamemanager ever.  [serializefield] private gameobject mainmenu; [serializefield] private gameobject replaybtn; [serializefield] private gameobject playbtn;  private bool gameend = false; private bool gamestarted = false; private bool playeractive = false; private bool gameover = false;  //getters setters start public bool playeractive {     { return playeractive; } }  public bool gameover {     { return gameover; } }  public bool gamestarted {     { return gamestarted; } } //to create state between gameover , main menu public bool gameend {     {return gameend; } } //getter setters end void awake(){     if (instance == null) {         instance = this;//this means current instance. 1 instance of class.     } else if (instance != this) {//if seocnd 1 gets created destroy bitch.         destroy (gameobject);     }     dontdestroyonload (gameobject);//allows game object persist between scene. dont need 1 scene.  }  // use initialization void start () {     replaybtn.setactive (false);  }  // update called once per frame void update () {  }  public void playercollided(){     gameover = true; }  public void playerstarted(){     playeractive = true;  } public void entergame (){     mainmenu.setactive(false);     gamestarted = true; } //when player dies start coroutine hold. public void restart (){     startcoroutine (holdexit());  } //the hole coroutine waits 2 seconds turns on menu. ienumerator holdexit (){     yield return new waitforseconds (2);     playbtn.setactive (false);     replaybtn.setactive (true);     mainmenu.setactive (true);     //add character movement location  } } 

so, when player hits object dies, loses ability touch colliders , falls through map, after 2 seconds main menu comes , play button replaced replay button. when press replay, need reset position, state of game, , ability collide.

i tried kinds of things. did component transform , tried call in coroutine , set there, couldnt figure out. tried changing position after etc after game managers state restart gets called position change occurs before main menu comes on because not being used in coroutine.

once thing though work, created new method,

  public void playerreset (){   if (gamemanager.instance.restart()){   //put new changes player here.      }   } 

the errors came across here not convert type void bool, assume cause trying if restart instance existed function, way restart function created isn't true or false - is.

i appreciate help. think im going try , make script side , have call class of player pull components, , manipulate them there maybe. augh. confusing. lmao.

you can set public transform variable on object, , in inspector, drag selected transform want call variable.

when that's done, can use transform variable in way want.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -