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 /
  • Help Room /
This question was closed Dec 05, 2016 at 08:23 PM by mircojanisch for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by mircojanisch · Dec 03, 2016 at 08:52 PM · scripting problemgameobjectreferencereference-other-object

Why is the this object referenced to itself?

Hi!

I have this piece of code. It's choosing a random prefab out of a array and find a script of its own type in it, which I earlier attached to the prefab. The problem now is that when I run this "newCross" is whyever referenced to itself and I have no plan why

     public void SpawnNextCrossroad(){
         for (int i = 0; i < hookPoints.Length; i++) {
             if (hookPoints[i].name != hookUsed) {
                 GameObject newCrossObj = crossroads[Mathf.RoundToInt(Random.Range(0, crossroads.Length - 1))];
                 Crossroad newCross = newCrossObj.GetComponent<Crossroad> ();
                 int distance = Mathf.RoundToInt(Random.Range (minDistanceBetweenCroasroads, maxDistanceBetweenCrossroads));
 
                 Vector3 posFac = hookPoints [i].position - calcPoints [i].position;
                 newCross.SpawnThis (posFac);
                 Instantiate (newCrossObj, (posFac * distance) + transform.position, Quaternion.identity);
             }
         }
     

Here a small sketch of my problem: alt text

zeichnung1.jpg (30.6 kB)
Comment
Add comment · Show 2
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 JedBeryll · Dec 04, 2016 at 07:01 AM 0
Share

I'm sorry, what? Sorry it's a bit confusing but shouldn't you get newCross from the newly created instance? You are getting it from the prefab right now.

avatar image mircojanisch JedBeryll · Dec 04, 2016 at 12:44 PM 0
Share

I added a sketch. This will help you

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by hexagonius · Dec 04, 2016 at 03:23 PM

that's because you're not doing what you describe in your sketches. you get the prefab, get ITS component and then instantiate it. You should do the GetComponent on the returned gameobject from the instantiate. BTW.

 crossroads[Random.Range(0, crossroads.Length)];

suffices, because Range here returns an int and excludes the last possible value

Comment
Add comment · Show 2 · 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 mircojanisch · Dec 05, 2016 at 08:22 PM 0
Share

Yeah, thats right. Here is how I solved it:

                 int whichCross = Random.Range(0, crossroads.Length - 1);
                 int distance = Random.Range ($$anonymous$$DistanceBetweenCroasroads, maxDistanceBetweenCrossroads);
                 Vector3 posFac = hookPoints [i].position - calcPoints [i].position;
                 GameObject newCrossObj = Instantiate (crossroads[whichCross], (posFac * distance) + transform.position, Quaternion.identity) as GameObject;
 
                 Crossroad newCross = newCrossObj.GetComponent<Crossroad> ();
                 newCross.SpawnThis (posFac);

And thank you for the hint. Didn't know that :)

avatar image hexagonius mircojanisch · Dec 06, 2016 at 07:25 PM 0
Share

you have it still wrong though. Length - 1 means they you will never get the last item. just use the length, Random accounts for that already

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

111 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 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 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

Hi everyone.need help with this. 1 Answer

CS0120 - Object reference required but it's already assigned. 0 Answers

Template object and script,GameObject scripts duplicated from a template game object 0 Answers

how to move a gameobject based on a dice roll 1 Answer

Having multiple UNET errors that I believe are caused by unity itself. Am I doing something wrong? 1 Answer


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