- Home /
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..
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.
ohk. in another script also i can call that function??
go to unityGE$$anonymous$$S.com for many long articles on this for beginners
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);
Your answer
Follow this Question
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