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 DGArtistsInc · Dec 24, 2011 at 10:59 PM · c#loops

Unity 3.5 C# Problem

This is a similar question to the last one. Since i couldnt figure out JavaScript for Flash i decided to try it in C#... Im not good at c#. Here is my script

     using UnityEngine;
     using System.Collections;
 
     public class PlayerWeapons : MonoBehaviour {
     
     void Start () {
         SelectWeapon(0);
     }
     
     void Update () {
         if(Input.GetKeyDown("1")){
             SelectWeapon(0);
         }
         if(Input.GetKeyDown("2")){
             SelectWeapon(1);
         }
     }
     
     void SelectWeapon(int index){
         For(int i=0; i<Transform.childCount; i++)
         {
             if(i == index)
             Transform.GetChild(i).gameObject.SetActiveRecursively(true);
             if(i != index)
             Transform.GetChild(i).gameObject.SetActiveRecursively(false);
         }
     }
     }

And here are the errors

Unexpected symbol i', expecting .'

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Unexpected symbol )', expecting ;'

Parsing error

Any help is appreciated

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

Answer by BerggreenDK · Dec 24, 2011 at 11:05 PM

Its around the statement with the FOR loop.

My first guess is that you need to spell FOR with small-caps. Not For but for

Secondly, you could try to make your IF-THEN structure a bit easier for yourself.

Here is the function if I should write it:

void SelectWeapon(int index)
{
   for(int i=0; i<transform.childCount; i++)
   {
     transform.GetChild(i).gameObject.SetActiveRecursively(i == index);
   }
}

Notice that I use the result of the comparison as the value for the SetActiveRecursively(bool)

If you prefered the IF structure you could do it like this:

void SelectWeapon(int index)
{
   for(int i=0; i<transform.childCount; i++)
   {
     if(i == index)
     {
         transform.GetChild(i).gameObject.SetActiveRecursively(true);
     } 
     else
     {
         transform.GetChild(i).gameObject.SetActiveRecursively(false);
     } 
   }
}
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 DGArtistsInc · Dec 24, 2011 at 11:30 PM 0
Share

One problem here is the two errors that still appear. The rest is excellent though.

An object reference is required to access non-static member UnityEngine.Transform.childCount' An object reference is required to access non-static member UnityEngine.Transform.GetChild(int)'

avatar image Bunny83 · Dec 25, 2011 at 12:55 AM 1
Share

Transform is a class name. Therefore it doesn't belong to a specific Transform instance. To access the Transform component of the gameobject where this script is attached to you have to use `transform` (lower case) which is a shortcut property of your script to access the Transform component of this object.

The usual way to get a reference to any other component on this GameObject is to use GetComponent

 GetComponent<Transform>()
 // is the same as
 transform

edit
I've edited the answer to correct the T. It should work now ;)

avatar image DGArtistsInc · Dec 25, 2011 at 08:55 PM 0
Share

YES it worked thank you very much for your help

avatar image BerggreenDK · Dec 25, 2011 at 08:58 PM 0
Share

thanks for specifying that detail Bunny! $$anonymous$$uch appriciated.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Initialising List array for use in a custom Editor 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Script to Tell if Circuit / Path is Complete 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