- Home /
Problem with multiple objects using the same script
I'm not sure if that has been answered yet, but I made a research and I can't find it. I'm new to Unity and I want to make a game that have some satellites orbiting around a character. The problem is that I need the satellites to descend in a random order. For that, I've created an extern object called SatManager that manages all teh random stuff and all satellites manouvres. But when I start the game, satellites go mad. I ried to fix that by adding the this target to all the actions of the satellites script, but they continue doing strange things: they orbitate around something unexpected, there doesn't appear the satellite I want, etc.
To make descend the satellite I want, I made the variable id. Id=1 goes for spain, id=2 goes for france, id=3 goes for italy and id=4 goes to greece. But when id=2 , italy descends, but france orbits around the character in a wrong y (upper)!
Please can someone help me?
Here are the scripts (named SatManager and OrbitSat):
For the SatManager:
static var state:int = 0; (In which state we can find the object: spawning the satellite, waiting for the satellite to be shot, etc.) private var id:int = 0; (the identifier or country of the satellite that's going to be spawn) public var numberSat:int; (how many satellites I've made so far) public var spain: OrbitSat; (identifier for the spanish satellite) public var france:OrbitSat; (identifier for the french satellite) public var italy:OrbitSat; (identifier for the italian satellite) public var greece:OrbitSat; (identifier for the greek satellite)
if (state==0){ id=Random.Range(1,numberSat+1); state=1 } (there are more actions there to make more than one satellite descend but I don't use them yet so they couldn't be the error)
if (state==1){ if (id==1){ if (spain.descend==0){ spain.descend=1; spain.transform.position.y=30; spain.transform.rotation.eulerAngles.y=204; spain.state=1; } } if (id==2){ if (france.descend==0){ france.descend=1; france.transform.position.y=30; france.transform.rotation.eulerAngles.y=204; france.state=1; } } if (id==3){ if (italy.descend==0){ italy.descend=1; italy.transform.position.y=30; italy.transform.rotation.eulerAngles.y=204; italy.state=1; } } if (id==4){ if (greece.descend==0){ greece.descend=1; greece.transform.position.y=30; greece.transform.rotation.eulerAngles.y=204; greece.state=1; } } state=2 }
For the satellites:
public var target : Transform; (the character) static var state:int = 0; (the same as the SatManager) static var descend:int = 0; (if the satellites had descended or nor) public var id:int = 0; (the country of the satellite)
function Update () { if (this.state==0){ this.transform.position.x=-103.68; this.transform.position.y=-101.67; this.transform.position.z=105.83; } if (this.state==1){ this.transform.Rotate(Vector3.up,Time.deltaTime*12); this.transform.position.x=target.transform.position.x+(Mathf.Sin((transform.rotation.eulerAngles.y+90)*Mathf.Deg2Rad)*30); this.transform.position.z=target.transform.position.z+(Mathf.Cos((transform.rotation.eulerAngles.y+90)*Mathf.Deg2Rad)*30); this.transform.position.y-=15*Time.deltaTime; if (this.transform.position.y<1.84){ this.transform.position.y=1.84; this.state=2; } } if (this.state==2){ this.transform.Rotate(Vector3.up,Time.deltaTime*12); this.transform.position.x=target.transform.position.x+(Mathf.Sin((transform.rotation.eulerAngles.y+90)*Mathf.Deg2Rad)*25); this.transform.position.z=target.transform.position.z+(Mathf.Cos((transform.rotation.eulerAngles.y+90)*Mathf.Deg2Rad)*25); } }
Thanks in advance!
I've managed to solve it! If someone have the same problem, remember that public variables can also communicate between two scripts.
Your answer
Follow this Question
Related Questions
Call same function in multiple scripts 4 Answers
Struggling with unity letting me attach scripts to game objects. 1 Answer
How can i set the same script on two GameObjects so the script will work on both objects same time ? 1 Answer
When the mouse is moving and the player is walking why the player is stuttering ? 0 Answers
How can i make the camera to lookat the next target and then rotate and move to the next target ? 0 Answers