• 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 /
  • Help Room /
avatar image
0
Question by Kagedmonkey40 · Feb 22, 2016 at 11:32 PM · accessing from any scriptaccessing scripts

Cant access an array of scripts

I'm new to C# but doing very well until I tried to create a group of objects that have public static variables, arrays and methods in them that I want accessed from another script.

The first part is my PuckContainerScript. I have 5 of these in my scene and each one controls 50 objects and variables. The script for it is below.

 using UnityEngine;
 using System.Collections;
 
 public class PucksContainerScript : MonoBehaviour {
 
     public static GameObject[] pucks;
     public static Vector3[] puckpos;
     public static float [] puckduration;
 
 
     void Start()
     {
         var mypucks = GetComponentsInChildren<SpriteRenderer>();
         int count = mypucks.GetLength(0);
         pucks = new GameObject [count];
         puckpos = new Vector3[count];
         puckduration = new float[count];
 
         count = 0;
         foreach (SpriteRenderer sp in mypucks)
         {
             pucks[count] = sp.gameObject;
             puckpos[count] = sp.transform.position;
             puckduration[count] = 0f;
             pucks[count].SetActive(false);
             count++;
         }
     }
 
     public static bool SetPuckPos(int who, float where)
     {
         bool retval = true;
         if (pucks[who] != null)
         {
             puckpos[who].x = where;
             pucks[who].transform.position = puckpos[who];
             pucks[who].SetActive(true);
         }
         else retval = false;
 
         return retval;
     }
 }

that part all works great! Where I am having trouble is another object with a script on it that I want to be able to access the PuckContainerScript from.

 using UnityEngine;
 using System.Collections;
 
 public class TapPointsHandlerScript : MonoBehaviour {
 
     public PucksContainerScript[] pucklists;
     public bool randomizepositions;
 
     void Update()
     {
         if (randomizepositions)
         {
             for (int clr = 0; clr < 5; clr++)
             {
                 float mytime = 1f;
                 for (int index = 0; index < 49; index++)
                 {
                     // this is where I have trouble! I can't seem to figure out how to actually access
                     // the variables, arrays and methods in my array of scripts.
                     pucklists[clr].SetPuckPos(index, mytime);
                     pucklists[clr].SetActive(true);
                     pucklists[clr].puckduration[index] = Random.Range(0.1f, 2f);
                     mytime += Random.Range(1f, 5f);
                 }
             }
             randomizepositions = !randomizepositions;
         }
     }
 }
 

I've tried all sorts of different combinations of how to access my PucksContainerScript but nothing really works. I get errors like:

Member 'PucksContainerScript.SetPuckPos(int, float)' cannot be accessed with an instance reference; qualify it with a type name instead

This has been driving me batty all weekend!

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 Kagedmonkey40 · Feb 23, 2016 at 12:07 AM

Nevermind......I figured it out!

Just now realized my issues was that I put the word "static" in the variables and methods I was trying to access.. Wasted my whole weekend because of that stupidity.

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 jinology · Apr 25, 2016 at 08:27 AM 0
Share

$$anonymous$$ay i know how u solve this problem? I am having similar problem as u. i cant access the Transfrom[] from another script. thx

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

46 People are following this question.

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

Related Questions

How to Reference Other Scripts' Variables in C# 1 Answer

Change of static variable 1 Answer

Access a script on a child on another gameobject in C#? 2 Answers

Accessing another scripts Variable 1 Answer

Cant Access Function in Other Script C# 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