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 /
avatar image
1
Question by TredOut · Nov 07, 2015 at 02:56 PM · arrayvariablestartfindgameobjectswithtagdeclaration

Declaring a variable in Start funciton

I'm following the unity documentation and I found exactly what I was looking for. The sample script in the documentation is // Search for game objects with a tag that is not used

     function Start () {
         var gos : GameObject[];
         gos = GameObject.FindGameObjectsWithTag("fred"); 
   
         if (gos.length == 0) {
             Debug.Log("No game objects are tagged with fred");
         }
 }

I did pretty much the same thing.

 using UnityEngine;
 using System.Collections;
 
 public class DoorOpen : MonoBehaviour {
 
     void Start () {
         var enemies : GameObject[];
         enemies = GameObject.FindGameObjectsWithTag("Enemy01");
 
     } 
 
     void Update () {
 
         if (enemies == null || enemies.Length == 0) {
             Destroy (this.gameObject);
             Debug.Log ("Enemies array has no enemies in it.");
         }
 
         if (enemies.Length > 0) {
             Debug.Log ("Enemies array has enemies in it.");
         }
     }
 }

However, this doesn't work. I get this error. Assets/DoorOpen.cs(7,29): error CS1525: Unexpected symbol :', expecting )', ,', ;', [', or ='

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

Answer by DeathDev · Nov 08, 2015 at 03:09 AM

There are two problems with this script.
1) Line 7 is written in JavaScript. It should instead be:

 GameObject[] enemies;

2) enemies is only defined inside of the start function, however you seem to want to access it outside of the function. To do this you will need to first declare the variable outside of any function.
Here's a fixed version of the script. :)

 using UnityEngine;
 using System.Collections;
  
 public class DoorOpen : MonoBehaviour {
  
     GameObject[] enemies;   //Note - outside of any function.
 
     void Start () {
         enemies = GameObject.FindGameObjectsWithTag("Enemy01");
  
     } 
  
     void Update () {
  
         if (enemies == null || enemies.Length == 0) {
             Destroy (this.gameObject);
             Debug.Log ("Enemies array has no enemies in it.");
         }
  
         if (enemies.Length > 0) {
             Debug.Log ("Enemies array has enemies in it.");
         }
     }
 }

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 TredOut · Nov 08, 2015 at 08:47 AM 0
Share

Thank you, it worked. It's kind of weird that unity has Js scripts in C# docs.

Anyways, now I encountered a different problem. GameObjects with Enemy01 tag still don't get added to the array. As soon as I start the Scene, the GameObject with this script gets deleted and I get the Debug.Log ("Enemies array has no enemies in it.");

These are the errors I get:

 UnityException: Tag: Enemy01 is not defined.
 DoorOpen.Start () (at Assets/DoorOpen.cs:9)

 NullReferenceException: Object reference not set to an instance of an object
 DoorOpen.Update () (at Assets/DoorOpen.cs:20)




avatar image
-1

Answer by Graham-Dunnett · Nov 07, 2015 at 02:58 PM

Your line 7 is written in Javascript, but the file is c#.

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 himanshugupta159 · Jan 22, 2019 at 06:38 AM

Written a blog related to concept of varibles and function the link is attched below: https://unfragilecoding.blogspot.com/2018/04/basicsof-programming-variables-and.html

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Declaring Variables in an Array of a Class 0 Answers

Possible to use an array value as the name of a variable? 2 Answers

Store Rigidbodies in Array 1 Answer

New Object Instantiation from Array working in Start() but not Update() 0 Answers

Create new variable name based on array index 3 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