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 betabob-2 · Aug 19, 2011 at 07:14 AM · collisionvariablereferencepublic variable

access a var from another script

I've seen this question asked dozens of times and have yet to find a solution to my problem. I'm new to JS and Unity as well so bear with me while I explain the situation.

I have two Scripts and a GameObject and a Prefab.

(script1)

The GameObject on GetButtonUp Fire2 (right-click) Instantiates a PreFab. After 15 PreFabs have been created I destroy the first one after each consecutive click.

(script2)

I now want the spawned PreFab to also destroy after a specific collision. The destruction works fine. My problem is that when the PreFab gets destroyed via a collision it doesn't know it in the script of the GameObject that spawns it.

What I am looking for:

If the object gets destroyed via a collision I want it to tell script1 that it has done so.

my code so far (not sure how to post code in a forum yet):

(Script 1, 'Creator', applied to empty gameObject 'Gun')

 var thePrefab : GameObject;
 public var MaxObjectCount = 0;
 var ForceX : float = 500.0;
 var ForceY : float = 500.0;
 var ForceZ : float = 500.0;
         
     function Update () {
     
          //spawn box
         if(Input.GetButtonUp("Fire2")){
         var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
     
         //velocity of spawned box
             instance.rigidbody.AddForce(Vector3(ForceX,0,0));
             instance.rigidbody.AddForce(Vector3(0,ForceY,0));
             instance.rigidbody.AddForce(Vector3(0,0,ForceZ));
             instance.name = "boxSpawn";
         //add to the total spawned
                 MaxObjectCount += 1;
                 checkhit();    
         }
     }
     
     function checkhit(){
     
          //after 15 box's have spawned kill the next one and create a new one
         if(MaxObjectCount == 16){
         
             Destroy(GameObject.Find("boxSpawn"));
         //subtract from the total spawned
             MaxObjectCount -=1;
         }
     }

(Script 2, applied to PreFab in Project Pane named 'BoxPrefab')

 function Update (){
 
       //not sure what to put here
     var killCount : Creator = gameObject.GetComponent(Creator);
 }
      //destroys the spawned box upon collision
 function OnCollisionEnter (boxHitKill : Collision) {
     if(boxHitKill.gameObject.name == "CollisionWallDestroyer"){
          Destroy(GameObject.Find("boxSpawn"));
 
     //where I want to subtract from the variable MaxObjectCount on the script 'Creator'
     Creator.MaxObjectCount -=1;
         }
 }


How can I access the current status of the variable 'MaxObjectCount' and affect it in Script2? I've read the documentation and a lot of forums posts but have yet to be able to make changes from one script onto another one since the second script is attached to a prefab that hasn't been spawned yet. I'm sure the solution is simple, but I am new to the scene and appreciate any help I can get. Thank you in advance!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by FTheCloud · Aug 19, 2011 at 09:13 AM

BroadcastMessage

I say the same thing every time someone asks this question

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
avatar image
0

Answer by betabob-2 · Aug 19, 2011 at 05:33 PM

My latest attempt for Script 2:

 var killCount : Creator;
 
 function OnCollisionEnter (boxHitKill : Collision) {
     if(boxHitKill.gameObject.name == "CollisionWallDestroyer"){
       Destroy(GameObject.Find("boxSpawn"));
 
         killCount.GetComponent(Creator).MaxObjectCount -=1;
     }
     }

The problem I have now (I think) is that I need to link the gameObject that has the creator script on it in the inspector. But since my object hasn't spawned I can't do that in the project pane and haven't figured out how to link it as soon as it's spawned.

I also dropped the update() function because I wasn't sure if I could setup a var in one function and have it be called in another, is that possible?

Comment
Add comment · Show 1 · 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 betabob-2 · Aug 23, 2011 at 01:26 AM 0
Share

function Update(){

var killCount : Creator = gameObject.GetComponent("Creator");

}

function OnCollisionEnter (boxHit$$anonymous$$ill : Collision) {

 if(boxHit$$anonymous$$ill.gameObject.name == "CollisionWallDestroyer"){

     Destroy(GameObject.Find("boxSpawn"));

         killCount = gameObject.GetComponent(Creator).$$anonymous$$axObjectCount -=1;

             }

}

$$anonymous$$y latest attempt, still just a variation from what I've been doing, have tried all kinds of other stuff but still no luck. I just can't get my prefab that spawns to ever actually 'get' the component or variable after it's created. I also added this script to the "gun" gameObject but even there it doesn't update the component or show the variable a 2nd time as I was expecting.

Any suggestions would be greatly appreciated.

avatar image
0

Answer by Spartan301 · Aug 23, 2011 at 01:45 AM

To answer your question about linking the creator script to the prefab object, all you need to do is select the prefab object in the project bar and drag the 'Gun' object to the killCount variable part of the script component. Also, you may be getting errors when you write

 var killCount : Creator;

since creator I don't beleive is a valid variables type. Instead try

 var killCount : Transform;

Finally when you use GetComponent, make sure to put the name of the guns script in quotation marks (""), otherwise it wont work. I think this should work, unless I am not fully understanding your situation.

Comment
Add comment · Show 1 · 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 betabob-2 · Aug 23, 2011 at 02:01 AM 0
Share

Ok, I think we are getting closer to the solution with this.

var killCount : Transform;

function OnCollisionEnter (boxHit$$anonymous$$ill : Collision) {

 if(boxHit$$anonymous$$ill.gameObject.name == "CollisionWallDestroyer"){

     Destroy(GameObject.Find("boxSpawn"));

         killCount = gameObject.GetComponent("Creator").$$anonymous$$axObjectCount -=1;

             }

}

I wasn't able to drag the Gun gameObject onto the script when it was still in the project pane, so I just placed it beneath my scene and that worked. So now my bullets spawn ok, they have the Gun script attached correctly but when the GetComponent occurs it tells me "NullReferenceException: Object reference not set to an instance of an Object". So where killCount = gameObject.GetComponent("Creator").$$anonymous$$axObjectCount -=1;

killCount is my var, "Creator" is my component and $$anonymous$$axObjectCount is the var within that component I am trying to access, no luck on that last part yet.

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

Javascript score problems whilst referencing scripts. 2 Answers

Specific enemy variable affects all enemies 2 Answers

public script variable in inspector 2 Answers

Set prefab to public variable of another script 1 Answer

Value to 0 on collision 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