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
1
Question by AndreyKhorkov · Jun 11, 2014 at 09:43 PM · c#instantiateprefabassignoop

how to set custom parameters to GameObjects and access them?

im trying to set a custom parameters to my GameObjects and access these parameters of one object by clicking on another object. So, i have this useless code that will help me figure out whats going on.

BaseScript.cs:

 using UnityEngine;
 using System.Collections;
 
 public class BaseScript : MonoBehaviour {
 
     public GameObject[] Bricks;
     public GameObject BrickPrefab;
     
     void Start () {
         Bricks = new GameObject[5];
 
         for (int i = 0; i <= 4; i++)
         {
             Bricks[i] = GameObject.Instantiate(BrickPrefab, new Vector3 (BrickPrefab.transform.position.x, i*1.1f, 0), BrickPrefab.transform.rotation) as GameObject;
             Bricks[i].GetComponent<BrickScript>().setCustomParameter("EXAMPLE" + i.ToString());
         }
     }
 
     void Update () {
     
     }
 
     public void myMethod() { 
         Debug.Log (Bricks[0].GetComponent<BrickScript> ().getCustomParameter ());
     }
 }
 

and BrickScript.cs:

 using UnityEngine;
 using System.Collections;
 
 public class BrickScript : MonoBehaviour {
 
     private string CustomParameter;
     public BaseScript baseScript;
     
     void Start () {
     
     }
 
     void Update () {
     
     }
 
     public void setCustomParameter(string CustomParameter) { 
         this.CustomParameter = CustomParameter;
     }
 
     public string getCustomParameter() { 
         return CustomParameter;
     }
 
     void OnMouseDown() {
         baseScript.myMethod ();
     }
 }

So, when i run the game it instantiates a five cubes "Bricks" out of my BrickPrefab. Each Brick has its own CustomParameter (EXAMPLE1, EXAMPLE2...EXAMPLE5) . I can see in the Inspector that these parameters are set properly if i make CustomParameter field public instead of private.

But, when i clicking on each Brick, i expecting "EXAMPLE0" in the console, but it throws me: "IndexOutOfRangeException: Array index is out of range. BaseScript.myMethod () (at Assets/BaseScript.cs:24) BrickScript.OnMouseDown () (at Assets/BrickScript.cs:26) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)"

Here is a screenshot: alt text

Im new to Unity3d and i guess it should be somethink simple. But i just dont understand how to assign everything properly.. May be there is a way to assign things programmatically? So please tell me what i did wrong... Thank you!

untitled.png (230.3 kB)
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 Kiwasi · Jun 12, 2014 at 09:53 AM 0
Share

I see nothing wrong with this script. I would suggest using statics for this type of manager class though.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · Jun 11, 2014 at 09:46 PM

You need to star debugging this.

Begin by printing the vale of Bricks[0] at the time access it.

If that throws an out of range exception then you need to figure out why its never getting initialized...

try putting a Debug right after you set Bricks in Start and see what it is there...

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 AndreyKhorkov · Jun 11, 2014 at 09:54 PM 0
Share

It didnt solve the problem. Just added this Debug.Log (Bricks[i].GetComponent ().getCustomParameter()); to that "for" loop where i instatntiating these Bricks. and the output is (EXA$$anonymous$$PLE0, EXA$$anonymous$$PLE1...EXA$$anonymous$$PLE5). So the problem is somewhere else. alt text

untitled.png (233.2 kB)

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

24 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

Related Questions

How to assign GameObject to a instantiated prefab via Script(C#)? 2 Answers

Can't set the random position for an Instance. 1 Answer

The variable othertransform of Prefab has not been assigned 2 Answers

Check if object is destroyed on level load, if so instantiate prefab? 1 Answer

how to instantiate object with content prefab and not him self 2 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