• 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 oz m · May 24, 2010 at 09:11 PM · variablesgameobjectsmultiplerespawn

How to use a script on multiple gameobjects, then change a variable for one of them, not the other.

I made a respawn script, to respawn an enemy once it has died. Everyt$$anonymous$$ng works so far, but I want to place respawn gameobjects multiple times in the level. Here's my script:

var beholder : Transform; var targetT$$anonymous$$ng : Transform; var Weapon: Transform; private var isClose : boolean = false; Beholder.t$$anonymous$$ng = targetT$$anonymous$$ng; Beholder.weapon = Weapon; var deathTimer : int = 0; var maxRespawns : int;

static var respawnCount : int = 0; function Update () {

 if(Vector3.Distance(targetT$$anonymous$$ng.position,transform.position) <= 30)
 {
     isClose = true;
 }
 else
 {
     isClose = false;
 }
 if(Beholder.death && isClose)
 {

     deathTimer++;

     if(deathTimer == 50 && respawnCount < maxRespawns)
     {
         deathTimer = 0;
         respawnCount++;
         Beholder.death = false;
         var newBeholder = Instantiate(beholder, transform.position, transform.rotation);
     }
 }

}


Edit:

By multiple objects, I meant that I wanted to make multiple versions of the object t$$anonymous$$s script is attached to so that they can have different respawnCount variables. That way i could have different respawn places that have seperate numbers of possible enemies to respawn. Then when it adds one to respawnCount of one respawn object, it doesn't to the other.

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 oz m · May 24, 2010 at 09:13 PM 0
Share

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by straydogstrut · May 24, 2010 at 09:27 PM

What specifically are you trying to change? If you place multiple gameObjects in your level and each has t$$anonymous$$s script, you can change the exposed variables for individual objects in the editor (just click on them and tweak the script values).

For changing values through code, you just need to reference the gameObject the script is attached to and the specific variable in the script. Take a look at Accessing Other Game Objects and Accessing Other Components from the scripting reference to get you started.

Comment
Add comment · Show 4 · Share
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 oz m · May 24, 2010 at 09:29 PM 0
Share
avatar image straydogstrut · May 24, 2010 at 09:50 PM 0
Share
avatar image oz m · May 24, 2010 at 09:51 PM 0
Share
avatar image oz m · May 24, 2010 at 09:55 PM 0
Share
avatar image
2
Best Answer

Answer by equalsequals · May 24, 2010 at 09:48 PM

Your problem here is that:

static var respawnCount : int = 0;

respawnCount is a static variable, and therefore is a member of the class, not the instance. In order to make it exposed, it should look like t$$anonymous$$s:

public var respawnCount : int = 0;

In doing t$$anonymous$$s you will not be able to access it by MyClassName.respawnCount anymore, but you will be able to edit it in your inspector panel in the Editor. T$$anonymous$$s way every object that t$$anonymous$$s script is attached to can have a different value for that variable.

Hope that helps.

==

Comment
Add comment · Show 4 · Share
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 straydogstrut · May 24, 2010 at 10:00 PM 0
Share
avatar image Maltus · May 24, 2010 at 10:21 PM 0
Share
avatar image straydogstrut · May 24, 2010 at 10:38 PM 0
Share
avatar image Joshua Falkner · Jul 28, 2010 at 08:20 PM 0
Share
avatar image
0

Answer by oz m · May 24, 2010 at 10:04 PM

My problem was actually Beholder.death. I made a public variable and set that to equal Beholder.death in Update() Now it respawns the enemy gameobjects from both Respawn gameobjects at the same time like I wanted, but it does that really fast! I'll tell you guys if I figure it out, but it would help if you could t$$anonymous$$nk about it too.

Comment
Add comment · Show 2 · Share
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 oz m · May 24, 2010 at 10:08 PM 0
Share
avatar image straydogstrut · May 24, 2010 at 10:39 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Trigger respawn point through player variables. 1 Answer

Difficulty Calling Variables from other scrtipts 1 Answer

One script for all variables? 0 Answers

Adding variables from all scripts 2 Answers

Reset GUI texture after respawn 1 Answer


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