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 Geordie3012 · Oct 21, 2010 at 05:02 PM · guibuttonbce0020

Using GUI Button to activate script instances

I am completely new to scripting of any sort and I have the following script set up to pull in rigidbody prefabs:

var spawnObject : Rigidbody;

function spawner () {

var newObject: Rigidbody = Instantiate (spawnObject, transform.position, transform.rotation);

}

I have attached the script to two seperate empty game objects - BallSpawnpoint and CubeSpawnpoint.

I am now wanting to activate both of these from a single GUI button with the following script:

function OnGUI () {

 if (GUI.Button (Rect (10,10,150,100), "Drop")) {

     GetComponent(spawn.spawner());
 }

}

which is returning the following error:

member 'spawner'.

member 'spawner'.> Assets/scripts/Button.js(7,36):

BCE0020: An instance of type 'spawn' is required to access non static member 'spawner'.member 'spawner'.

Can someone help please?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by skovacs1 · Oct 21, 2010 at 05:32 PM

You cannot access the function spawner because your script doesn't know where to get it from.

Also GetComponent gets a component attached to the specified gameobject (in this case the script to which your GUI script is attached) of the type specified. A function is not a type of component.

If your function were static, you could access the function spawn.spawner (static function spawner in the script spawn.js), but the variable transform would be invalid to use within the static function because it is not static (as would spawnObject, but depending on your use case is less problematic).

Because you want to call the function on instances of the script, you will need to get the instance(s) of the script attached to your other GameObjects like so:

button.js

function OnGUI () {
    if (GUI.Button (Rect (10,10,150,100), "Drop")) {
        //Find the spawners. Requires they be tagged Spawner
        //If these don't change, you could store this on Start
        //or you could store them publicly (and even statically if you like).
        var spawners : GameObject[] =
            GameObject.FindGameObjectsWithTag("Spawners");
        for(var spawner : GameObject in spawners) {
            var script : spawn = spawner.GetComponent(spawn);
            if(script) //If they have this script attached call the function
                spawn.spawner(); 
        }
    }
}

As an alternative, depending on your use case, in stead of having the spawner function in a script attached to your spawner GameObjects, you could store the GameObjects and the function within the button or another controller script that is easier to access (maybe as a static function?) and then have the spawner function loop through the GameObjects that are stored with it and instantiate, like so:

spawnTarget.js (Attached to your spawners)

var spawnObject : Rigidbody;

spawn.js (Doesn't really need to be attached to anything, but it can be)

static var spawners GameObject[];

static function spawner() { for(var spawner : GameObject in spawners) { var script : spawnTarget = spawner.GetComponent(spawnTarget); if(script) { var target : Rigidbody = script.spawnObject; var newObject: Rigidbody = Instantiate(target, spawner.transform.position, spawner.transform.rotation); } } }

button.js (Attached to something)

function OnGUI () {
    if (GUI.Button (Rect (10,10,150,100), "Drop")) spawn.spawner();
}
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 Geordie3012 · Oct 25, 2010 at 09:46 AM 0
Share

I have tried copying in the first script and I'm getting the following errors:

Assets/scripts/Button.js(6,61): BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.GameObject[]'.

and

Assets/scripts/Button.js(10,23): BCE0020: An instance of type 'spawn' is required to access non static member 'spawner'.

avatar image skovacs1 · Oct 25, 2010 at 02:02 PM 0
Share

Sorry. was calling the wrong version of FindWithTag. It has been corrected.

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

No one has followed this question yet.

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Making a Hover function for a button or toolbar from code. 2 Answers

Question about positioning GUI. 1 Answer

Button reaction time problem 1 Answer

How to change a GUI button texture through scripting 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