Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Digital-Phantom · Jan 07, 2015 at 02:02 PM · javascriptprefabvariablebooleanotherscript

Change Variable on Another Script

I have 2 scripts running in my game, one attached to a transform (spawnHandler) called "RandomSpawnRandomLocation"

 #pragma strict
 
 var spawnPoint : Transform[]; // Create list of possible spawn locations
 var wepType : GameObject[]; // Creat list of possible weapons to be spawned
 var wepPresent : boolean = false;
 
 function Start()
 {
     SpawnWeapons(); //Run SpawnWeapons coroutine
 }
 
 function Update()
 {
     if(wepPresent == false) //Check to see if weapon is present
         {
             SpawnWeapons(); //If no weapon is present run SpawnWeapons coroutine
         }
 }
  
 function SpawnWeapons() //Spawns a random weapon at one of possible spawn locations
 {
     var randomLoc : int = Random.Range(0,4); //Pick random number between 1 and 4
     var randomWep : int = Random.Range(0,4); //Pick random number between 1 and 4
     
     Instantiate(wepType[randomWep], spawnPoint[randomLoc].position, spawnPoint[randomLoc].rotation);
     //Pick random weapon depending on which number was generated by 'randomWep'
     //Spawn weapon at location depending on which number was generated by 'randomLoc'
     
     wepPresent = true;
 }

The other is a simple self destruct script attached to the gameObjects/prefabs that get spawned - "DestroySelfOnContact" (the objects this script is attached to are prefabs and get spawned/destroyed multiple times)

 #pragma strict
 
 function OnTriggerEnter()
 {
     Destroy (gameObject);
 }

How do I get the DestroySelfOnContact script to change the 'wepPresent' variable on the RandomSpawnRandomLocation script ?

Would I be better off having the destroy script attached to my player object as that is always present in the scene ?

???

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by _Yash_ · Jan 07, 2015 at 02:17 PM

Sorry i'm not good at javaScript but you can easily find javascript equivalent.

in DestroySelfOnContact script you can

 RandomSpawnRandomLocation otherScript;
 void Start(){
     otherScript = GameObject.find("spawnHandler").GetComponent<RandomSpawnRandomLocation>();
 }
 
 void OnTriggerEnter()
 {
     Destroy (gameObject);
     // now you can do otherScript.wepPresent = false/true
 }
Comment
Add comment · Show 3 · 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 Digital-Phantom · Jan 07, 2015 at 03:52 PM 0
Share

var wepPresent : boolean = false;

Has now been changed to -

public var wepPresent : boolean = false;

And my destroySelfOnContact script now is -

 #pragma strict
 
 public var getSpawnHandler :  GameObject;
 
 private var randomWeaponRandomLocation : RandomWeaponRandomLocation;
 
 
 
 function Awake ()
 {
     randomWeaponRandomLocation = getSpawnHandler.GetComponent(RandomWeaponRandomLocation);
 }
 
 
 function OnTriggerEnter()
 {
     Destroy (gameObject);
     randomWeaponRandomLocation.wepPresent = false;
 }

I'm getting no compile errors, but nothing is actually happening. Shouldn't this now be affecting/changing the variable on the 'other' script

???

avatar image Digital-Phantom · Jan 07, 2015 at 05:48 PM 0
Share

Anybody ???

avatar image _Yash_ · Jan 08, 2015 at 02:42 AM 1
Share
 randomWeaponRandomLocation.wepPresent = false;
 Destroy (gameObject);

Try destroying after you make ur changes...

avatar image
0

Answer by Shrikky23 · Jan 08, 2015 at 02:53 AM

First create an Object to the script, I will quote the other person's reply as a reference.

Modify the above idea, make the var as public .

public var randomWeaponRandomLocation : RandomWeaponRandomLocation;

Check if the gameobject is assigned, if its assigned properly, then make sure you change the value before you destroy the object

Comment
Add comment · 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

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

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prefabs and variables 1 Answer

void cannot be used in a boolean context 1 Answer

[Urgent]Changing variables of an object AFTER instantiation 2 Answers

Variables are getting permanently stored in prefabs 2 Answers

How to stop a boolean from going back to false 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