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 Ekta-Mehta-D · Mar 13, 2013 at 06:16 AM · variable

Access variable in another script beginner

I am currently learning the concepts of unity.. so sorry if i have asked you something wrong. But i want to make my concepts clear. In my case i have one script in that i am instantiating bulletprefab and at the same time i want to generate sphere of the random color. so i have created prefab with different color which i am going to instantiate randomly. Now i want to change that prefab every time i shoot.

so in shooting script start() function again i need to to generate random prefab. so what can be the best way to avoid duplicate lines of code ??

code :

 function Start () {
     futureBubble = GameObject.Find("Sphere(FutureBubble)"); // Next bubble to be shoot
     if(futureBubble != null)
     {
         random = Random.Range(0 , bubblePrefabs.Length - 1);
         Instantiate(bubblePrefabs[random] , futureBubble.transform.position , Quaternion.identity);
     }
 }

 function Update ()
 {
     var spawnPoint : GameObject = GameObject.Find("Bullet");         // Current Bullet Bubble to be shoot
         
     if(Input.GetMouseButtonDown(0))     
     {     
          if(spawnPoint != null)
          {
              Instantiate(bulletSphere, spawnPoint.transform.position,spawnPoint.transform.rotation);
 
                      // here again i need another color prefab
          }            
     }
 }

Thanx for your support and help..

Comment
Add comment · Show 4
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 theodox · Mar 13, 2013 at 06:23 AM 0
Share

If you just want to repeat the same operation in line 20 that you are doing in lines 5-6, move lines 5-6 to a new function and call it in both places:

     function Start () {
             futureBubble = GameObject.Find("Sphere(FutureBubble)"); // Next bubble to be shoot
             if(futureBubble != null)
             {
 CreateBubblePrefab(futureBubble.transform.position);
             }
         }
          
         function Update ()
         {
             var spawnPoint : GameObject = GameObject.Find("Bullet");         // Current Bullet Bubble to be shoot
          
             if(Input.Get$$anonymous$$ouseButtonDown(0))  
             {    
                 if(spawnPoint != null)
                 {
                  Instantiate(bulletSphere, spawnPoint.transform.position,spawnPoint.transform.rotation);
     CreateBubblePrefab(futureBubble.transform.position);
                 }           
             }
         function CreateBubblePrefab (spawn_pos)
         {
         random = Random.Range(0 , bubblePrefabs.Length - 1);
         Instantiate(bubblePrefabs[random] , spawn_pos , Quaternion.identity);
         }
         }
     
 

Note: this is not tested code! But it illustrates the idead. Apologies for formatting.

avatar image Ekta-Mehta-D · Mar 13, 2013 at 06:26 AM 0
Share

ohk. in another script also i can call that function??

avatar image Ekta-Mehta-D · Mar 13, 2013 at 06:46 AM 0
Share

Ohk thanks sir..

avatar image Fattie · Mar 13, 2013 at 10:20 AM 0
Share

go to unityGE$$anonymous$$S.com for many long articles on this for beginners

1 Reply

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

Answer by Seth-Bergman · Mar 13, 2013 at 06:38 AM

since it is public (by default), yes, once you have a reference in the other script to this script.. for example:

  var otherScript = GameObject.Find("NameOfObject").GetComponent(NameOfScript); //(notice no quotes!)

(on another object) or

   var otherScript = GetComponent(NameOfScript); 


(on the same object)

once you have the script reference, you can call the function as:

  otherScript.CreateBubblePrefab(futureBubble.transform.position);
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

13 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

Related Questions

Clearing a destination... 1 Answer

Store temp var / invert var 1 Answer

C# Accessing Variables from Class Variable 1 Answer

C# Declaring Variables Within If Statements 2 Answers

Prefab (Project Folder) to a GameObject (on the Scene) 2 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