Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by brainstorm18 · Jul 20, 2016 at 08:50 PM · script.multiple-objects

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image brainstorm18 · Jul 22, 2016 at 07:05 AM 0
Share

I've managed to solve it! If someone have the same problem, remember that public variables can also communicate between two scripts.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges