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 /
This question was closed Oct 27, 2011 at 02:29 PM by AquaGeneral for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by AquaGeneral · Feb 06, 2010 at 08:01 AM · optimizationbestpractices

Cleaner scripting

I have posted this in the forums but I got no response. Hopefully I will have better luck here.

Copy and paste :)

I seem to have hit a brick wall in my game. I am trying to create a simple editor mode that allows the player to select an object from a selection grid, it is set as a cursor. After that if the player presses spacebar the object is then called in from the sky.

The best example of what I want is this video: Cortex Command run-through (YouTube)

However what I have in mind is a mix of what you see in this editor - in the way that at the start of a match in Cortex Command your selected object is your cursor. However instead of it being placed directly as you position it, it is sent from the sky. Also at the moment (at least) I am not interested in allowing the player to call in multiple objects at the same time. So in that sense it is also simpler.

Here is part of my code (the script is named Cursor):

var cursor1 : Transform; //Prefabs var cursor2 : Transform; //Prefabs var cursor3 : Transform; //Prefabs

static var NoCursorIsAlive = true; static var Cursor1Spawn : boolean = false; static var Cursor2Spawn : boolean = false; static var Cursor3Spawn : boolean = false;

function Update () { if (NoCursorIsAlive == true && Spawner.selectedPrefab == 1) { var prefab1 : Transform = cursor1; //Start off with the first prefab var Spawned1 = Instantiate(cursor1, transform.position, transform.rotation); //Spawn the cursor and keep that spawned cursor trackable Spawned1.parent = transform; //Add the cursor as a child NoCursorIsAlive = false; //There is now a cursor alive Cursor1Spawn = true; }

if (Cursor1Spawn == true && Spawner.selectedPrefab == 2) { var prefab2 : Transform = cursor2; //Start off with the first prefab var Spawned2 = Instantiate(cursor2, transform.position, transform.rotation); Spawned2.parent = transform; //Add the cursor as a child Cursor2Spawn = true; Cursor1Spawn = false; }

if (Cursor2Spawn == true && Spawner.selectedPrefab == 3) { var prefab3 : Transform = cursor3; //Start off with the first prefab var Spawned3 = Instantiate(cursor3, transform.position, transform.rotation); Spawned3.parent = transform; //Add the cursor as a child Cursor2Spawn = false; NoCursorIsAlive = true; }}


There is a spawner script is the second part of this editor. It looks for input and spawns the object at the current position of the cursor, but of course I can easily call it in from the sky later (that part isn't important). As well as a special script for each separate prefab that is spawn-able, as you can see I really need to improve the efficiency of my coding skills.


Spawner Script:

var prefab1 : GameObject; var prefab2 : GameObject; var prefab3 : GameObject;

var movespeed : float = 5;

static var selectedPrefab : int = 1; var ThisOneIsForYou = 1;

function Update () {
//On press Z or X add to the selected prefab index if (Input.GetButtonDown("EditorCyclePrefab")) { if (selectedPrefab > 2) { selectedPrefab = 1; ThisOneIsForYou = 1; } else { selectedPrefab++; ThisOneIsForYou++; }}

 if (selectedPrefab == 1) {
     if (Input.GetButtonDown("Spawn")) {
         Instantiate(prefab1, transform.position, transform.rotation);
 }}

 if (selectedPrefab == 2) {
     if (Input.GetButtonDown("Spawn")) {
         Instantiate(prefab2, transform.position, transform.rotation);
 }}

 if (selectedPrefab == 3) {
     if (Input.GetButtonDown("Spawn")) {
         Instantiate(prefab3, transform.position, transform.rotation);
 }}

 //Right Move
 if (Input.GetAxis("Horizontal") < 0) {
     transform.position.x += movespeed * Time.deltaTime;
 }

 //Left
 if (Input.GetAxis("Horizontal") > 0) {
     transform.position.x -= movespeed * Time.deltaTime;
 }

 //Up
 if (Input.GetAxis("Vertical") > 0) {
     transform.position.y += movespeed * Time.deltaTime;
 }

 //Down
 if (Input.GetAxis("Vertical") < 0) {
     transform.position.y -= movespeed * Time.deltaTime;
 }

}


Capsule Delete Script:

function Update () {
    if (Spawner.selectedPrefab == 2) {
        Destroy(gameObject);
}}

Is there a better method for this? Can anyone give me some clues on how I should go about it in a cleaner fashion? Bear in mind I only am displaying these scripts to show you what I am doing and hopefully make it easier for you to recommend a different method.

Thank you for your time.

Comment
Add comment · Show 1
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 Benproductions1 · Nov 28, 2012 at 04:39 AM 0
Share

If this question is closed, please close it.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Memory management and C# do's and don'ts? 5 Answers

what's the best way to reduce draw calls? 9 Answers

GL drawing optimization 0 Answers

Composite Design Pattern in Unity, Redundant? 1 Answer

Low Priority Methods In LateUpdate? 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