Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Intiaani_Jones · Nov 24, 2015 at 07:45 PM · scripting probleminstantiate

Accessing the script of an instantiated object

Hi,

I'm making a sort of strategy game and I'm currently working on the selection and moving stuff, but sadly I have encountered a problem that I've been unable to solve. In the game I have 4 balls that I have manually put there and a number of instantiated ball all made from the same prefab (every ball is perfectly identical). And what's happening is that I can select and deselect (change a bool from false to true) the manually put balls just fine but nothing happens in the instantiated balls. Even if I press one of the instantiated balls, only the manually placed balls are selected.

To spawn the balls I currently use either this:

void Start()

{

//for (int i = 0; i < 1000; i++) {

// Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);

//}

}

Or this:

void Start ()

{

GameObject newObject = (GameObject)Instantiate (PALLOPrefab); newObject.GetComponent ().valittu = true;

}

And this is where the magic doesn't happen, I think. The bool "valittu" (chosen) determines whether the object is selected or not:

void Update()

{

if (Input.GetMouseButtonDown (0)) {

         RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10)), Vector2.zero);
         
         if (hit.collider != null) {
     
             if (hit.transform.tag == "Unit1") {
                 Debug.Log("lol");
                 foreach (GameObject go in GameObject.FindGameObjectsWithTag("Unit1")) {                    

                     go.GetComponent<Ukko> ().valittu = true; 
                     valittu=true;
                     go.GetComponent<Ukko> ().novoijuma = true; 
                 }
             }
         } 

}

This script is attached to an object responsible for controlling stuff and each ball has some other script. I'm quite sure that the cause is in the "foreach" bit, since the "lol" prints out just fine when clicking on Instantiated ball, but I have no idea how to fix it.

Thanks for help!

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 ExamplesAreExamples · Nov 25, 2015 at 03:45 PM

My guess is that your prefab isn't tagged "Unit1".

Also, if your Ukko component sets valittu to false in Start, that will happen after you set valittu to true in your second Start.

So you suspect something strange is going on here:

 foreach (GameObject go in GameObject.FindGameObjectsWithTag("Unit1")) 
 {                    
     go.GetComponent<Ukko> ().valittu = true;
     valittu=true;
     go.GetComponent<Ukko> ().novoijuma = true;
 }

Let's move it into a method, break it up and try to make it a little verbose.

 void SelectMenInUnit1()
 {
     var taggedUnit1 = GameObject.FindGameObjectsWithTag("Unit1");
     
     WarnIf (taggedUnit1.Length == 0, "Could not find any game objects tagged Unit1");
     
     foreach (var u1 in taggedUnit1) 
         SelectMan (u1);
 }
     
 void SelectMan(GameObject go)
 {
     // Ukko means man, dude, guy etc. It's finnish. I guess, the man is also a ball.
     
     var man = go.GetComponent<Ukko> ();
     WarnIf(!man, "GameObject " + go.name + " has no Ukko component - skipping");
     
     if (man) 
     {
         // If there's a component representing a man sitting on the ball in Unit1,
         // let it know he's chosen, for gods sake. Additionally, let myself 
         // remember I told a man he's chosen.

         // Glossary:
         //   valittu means selected or chosen in finnish
         //   novoijuma means "oh, for gods sake" in finnish, I believe.
         
         man.valittu = true;
         man.novoijuma = true;
         valittu = true;
     }        
 }
 
 void WarnIf(bool condition, string message)
 {
      if (condition)
          Debug.LogWarning(message, this);
 }

It's basically the same code as you had before, but more verbose and with explicit warnings where no Units were found or no Ukko component was on a Unit game object. Perhaps it can help you find the issue. It wasn't explicitly clear what correlation there was between Unit1, balls and men so I wrote a comment to try and capture what should happen.

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 Intiaani_Jones · Nov 26, 2015 at 04:36 PM 0
Share

The prefab is indeed tagged as "Unit1". The only difference that I can see between the balls placed in the editor and the ones prefabbed is the colour of the cube next to the object's name in the inspector. The ones I placed have a light blue cube and the prefabbed have a red/green/blue, but that can't have anything to do with this?

Just replacing my code with yours makes no difference, I still can't select the prefab balls, but it does throw an "object reference not set to an instance of an object" on line 94, which doesn't exist. $$anonymous$$aybe tomorrow I'll have more time to mess with the code and see if I might be able to fix the problem or move on to another way of selecting.

On a side note, your comments were pretty much spot on.

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

37 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

Related Questions

Assign variables on Instantiation? 0 Answers

Add Component with Script Data 1 Answer

How to change the Bool for only a single prefab GameObject creating with Instantiate? 2 Answers

Is there a way to edit an uninstantiated object? 1 Answer

Instantiating object randomly withing the boundaries of an object. 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