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 oliver-jones · Nov 26, 2010 at 09:19 PM · variablestatictower-defenseprivateindependent

Need Help With My GAME!

I'm trying to build a 'tower defence' type game, and the game works fine with one turret. But when I have duplicate turrets dotted around the map, the turrets freak out.

Watch My YouTube Video: http://www.youtube.com/watch?v=6xCRZZiu9pU

Basically, what happens is when you select a turret, you have the ability to upgrade the firing range, and change many variables. But when I duplicate the turret, the variables get all mixed up (I'm guessing because they are static, as I need to share them).

And of course, duplicating turrets is important as the user will eventually be able to drag on turrets from a GUI - thus duplicating a prefab (I would imagine).

How can I go about making each turret variables unique to them selves? I really need this folks! Below are my script if you want to get an idea how things are laid out:

ClickObject: (script that picks up the mouse click - attached to parent)

#pragma strict

var clicked : boolean = false; var hit : RaycastHit; static var ClickedOn : boolean = false;

function Update() { if(Input.GetMouseButtonDown(0) && collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, Mathf.Infinity)) { clicked = !clicked; if(tag == "Select") tag = "Unselect"; else tag = "Select"; //Debug.Log("clicked" + (clicked? "" : " off")); } ClickedOn = clicked; }

Control: (variables that controls the turrets state - attached to child)

static var Play = false; static var Radius : int = 10; static var ModerniseLevel : int = 1; static var ModernisePlay : boolean = false; static var DisplayCamera : boolean = true; static var TurretClicked : boolean = true; var TurretCam : Camera;

function Start () { TurretCam.enabled = true;
}

function Update () {

 //TurretClicked = ClickObject.ClickedOn;
 collider.radius = Radius;
 //TurretClicked = ClickObject.ClickedOn;
 print(TurretClicked);

 if(ModerniseLevel == 2 && ModernisePlay == true){
     animation["SpawnMissile"].speed = 0.5;
     animation.wrapMode = WrapMode.Once;
     animation.Play("SpawnMissile");
     ModernisePlay = false;
 }

}

RadiusTexture: (positions sphere around turret according to radius - attached to same child)

var RadiusObject : Transform; var RadiusSphere : Transform; private var CurrentSize : float;

private var ScaleX = 3.1; //2 private var ScaleY = 0; //0 private var ScaleZ = 3.4; //2.3

private var ScaleXDiv : float = 0; private var ScaleYDiv : float = 0; private var ScaleZDiv : float = 0;

private var ScaleXNew : float = 0; private var ScaleZNew : float = 0;

private var Clicked = false; var MouseOverClicked = false;

var Texture = true;

function Update () { if(ClickObject.ClickedOn == true) { LeftMenu.TurnOn = true; if(Texture == true) { CurrentSize = collider.radius;

         ScaleXDiv = (ScaleX * CurrentSize);
         ScaleXNew = ScaleXDiv / 10;

         ScaleZDiv = (ScaleZ * CurrentSize);
         ScaleZNew = ScaleZDiv / 10;

         RadiusObject.transform.localScale = Vector3(ScaleXNew,ScaleY,ScaleZNew);
     }
     else
     {
         CurrentSize = collider.radius;
         RadiusSphere.transform.localScale = Vector3(CurrentSize * 3,CurrentSize * 3,CurrentSize * 3);

     }
     //Clicked = true;
 }
 else if(ClickObject.ClickedOn == false)
 {

     LeftMenu.TurnOn = false;
     RadiusObject.transform.localScale = Vector3(0,0,0);
     RadiusSphere.transform.localScale = Vector3(0,0,0);
     //Clicked = false;  
 }

}

LeftMenu: (GUI)

private var myLeft:float = 10; private var myTop:float = 100; private var myWidth:float = 150; private var myHeight:float = 40;

var prefab : GameObject; var gridX = 5; var gridY = 5; var spacing = 2.0;

var isDragging:boolean = false; var hit : RaycastHit;

var Power = 10; var Strength = 10;

var TurretCamera : Texture; static var TurnOn = false;

function OnGUI() { //Turret Camera Window if(TurnOn == true){ GUI.DrawTexture(Rect(Screen.width - 300, Screen.height - 300,260,260), TurretCamera, ScaleMode.StretchToFill, false, 30.0f); }

 if (GUI.RepeatButton(Rect(myLeft, myTop, myWidth, myHeight), "Moving Button" ))
     isDragging = true;

 if (isDragging){
     myLeft = Input.mousePosition.x - myWidth*0.5;
     myTop = Screen.height - (Input.mousePosition.y + myHeight*0.5);
 }

 if(Controller.OrbitCamVar == false){
     if (GUI.Button(Rect(10,10,50,50),"3D")){
         Controller.OrbitCamVar = true;
     }
 }
 else if(Controller.OrbitCamVar == true){
     if (GUI.Button(Rect(10,10,50,50),"2D")){
         Controller.OrbitCamVar = false;
     }
 }

 if(TurnOn == true)
 {
     GUI.BeginGroup (new Rect (2, Screen.height - 300, 170, 300));
     GUI.Box(Rect(0,0,170, 300),"Turret Info");
     GUI.Label (Rect (10, 30, 200, 30), "Name: Machine Turret");
     GUI.Label (Rect (10, 80, 200, 30), "Radius: " +Control.Radius); //Max 40!
     if (GUI.Button (Rect (170 - 65, 80, 60, 20), "Upgrade")){
         if(Control.Radius < 40){
             Control.Radius += 3;
         }
         else
         print("Max Radius Reached!");
     }
     GUI.Label (Rect (10, 110, 200, 30), "Power: " +Power);
     if(GUI.Button (Rect (170 - 65, 110, 60, 20), "Upgrade")){
         Power += 5;
     }
     GUI.Label (Rect (10, 140, 200, 30), "Strength: " +Strength);
     if(GUI.Button (Rect (170 - 65, 140, 60, 20), "Upgrade")){
         Strength += 5;
     }

     if(GUI.Button (Rect (170/2 - 130/2, 250, 130, 30), "Modernise")){
         Control.ModerniseLevel += 1;
         Control.ModernisePlay = true;
     }

     GUI.EndGroup();

 }

}

If you do have an idea of how to help me, please give an example - my scripting it still not very strong. I would appreciate this to whom ever can help!

Ollie

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
Best Answer

Answer by _Petroz · Nov 27, 2010 at 01:23 AM

Communication between scripts which are connected to the same GameObject is easy. For example you could change ClickObject 'Clicked' to non static and then access it from other scripts like this:

var clickObject : ClickObject; clickObject = gameObject.GetComponent(ClickObject);

if (clickObject.Clicked) { // This turret is clicked }

Comment
Add comment · Show 4 · 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 oliver-jones · Nov 27, 2010 at 12:56 PM 0
Share

Okay, but what about if you need to connect to a script that in a different child of that object? And what about connecting to a script that isn't even in the gameobject at all? Thanks For The Reply

avatar image oliver-jones · Nov 27, 2010 at 01:07 PM 0
Share

Also, I tried just adding this to my Click - and I get an Exceptional Error : Object referenced not set to an instance of an object? I'm guessing it returns Null then? Why...

avatar image oliver-jones · Nov 27, 2010 at 08:06 PM 0
Share

I'm over the moon - I'm very happy with my game now, its all working how I wanted it to. Thanks Everyone!

avatar image _Petroz · Nov 27, 2010 at 08:55 PM 0
Share

Glad to hear it's all working for you. By the way, you're game looks pretty cool, best of luck! :)

avatar image
0

Answer by Jay_Adams · Nov 27, 2010 at 01:12 AM

I'm a hack so just throwing out ideas.

first is your turret a prefab?

Second, maybe just have a seperate script for each turret (I suppose you wont have too many).

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 Pete Roobol · Nov 27, 2010 at 01:45 AM

Hi, Yup too many static vars, a static var is global so if you set it, it will update the same in all script instances of that script. So loose the static unless it needs to be global.

Use GetComponent(OtherScript) to get other script instance,

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

eg if(gameObject.GetComponent(ClickObjectScript).clicked){...

Loose the tower instance statics should sort the chao

Good luck :)

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 oliver-jones · Nov 27, 2010 at 04:50 PM 0
Share

Thank you, what do you mean by "Lose the tower instance statics should sort the chao"?

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

Specific enemy variable affects all enemies 2 Answers

public private and static variables in js 1 Answer

Prefab's Script affecting all the Prefab 1 Answer

Static, yet Private, Variables? 1 Answer

Manipulating Variables Over Multiple Scripts 0 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