• 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
Question by JCGameArt · Oct 14, 2014 at 08:39 PM · c#instantiate

Intantiate problems

I'm trying to generate random obstacles for a temple runner type game, the main problem though is the issue surrounding "temp".

I'm crazy new at scripting so anyhelp would be great. Here's the script so far.

 using UnityEngine;
 using System.Collections;
 
 public class RockSpawnScript : MonoBehaviour {
 
     public GameObject Rock_001;
     public GameObject Rock_002;
     public GameObject Rock_003;
     public GameObject Rock_004;
     public GameObject Rock_005;
 
     float timeElapsed = 0;
     float spawnCycle = 3f;
     int chooser = Random.Range (1,5);
 
     
     void Update () {
         timeElapsed += Time.deltaTime;
         if (timeElapsed > spawnCycle)
         {
 
         Vector3 pos = temp.transform.position;
     
 switch (chooser)
 {
 case 1:
         temp = (GameObject)Instantiate(Rock_001);
         pos = temp.transform.position;
         temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);
     break;
 case 2:
         temp = (GameObject)Instantiate(Rock_002);
         pos = temp.transform.position;
         temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);
     break;
 case 3:
         temp = (GameObject)Instantiate(Rock_003);
         pos = temp.transform.position;
         temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);
     break;
 case 4:
         temp = (GameObject)Instantiate(Rock_004);
         pos = temp.transform.position;
         temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);
     break;
 case 5:
         temp = (GameObject)Instantiate(Rock_005);
         pos = temp.transform.position;
         temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);
     break;
 default:
         temp = (GameObject)Instantiate(Rock_001);
         pos = temp.transform.position;
         temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);
     break;
             }
         }
     }
 }
Comment

People who like this

0 Show 4
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 gjf · Oct 14, 2014 at 03:22 PM 0
Share

perhaps you could elaborate on "the main problem though is the issue surrounding "temp"."

what is the problem?

some things to think about:

this line: int chooser = Random.Range (1,5); gets executed exactly ONCE - that's probably not what you intended, right?

you'll be better off looking at lists/arrays to store your rock objects/prefabs - then instead of an ugly switch (bonus points for not doing an even uglier if/else, although the generated code isn't that different... anyway, i digress) you can simply index the list/array with the random number to get the required object/prefab.

all that other code is duplicated and could just as easily be done after you've got the rock prefab/object.

something like:

 temp = Instantiate(Rocks[Random.Range (1,5)]) as GameObject;
 pos = temp.transform.position;
 temp.transform.position = new Vector3(Random.Range(20, -20), pos.y, pos.z);

way less code, way fewer problems.

obviously, none of that answers the main problem...

avatar image JCGameArt · Oct 21, 2014 at 11:28 AM 0
Share

Hey there, sorry for the lack of info, the issue was that temp wasn't an assigned local variable. I see my noobish scripting really shows eh?

i've attempted your solution to solve the problem and it seems to be working fine except that error still shows as you predicted. Here's the line of code, hope it comes out properly...

 using UnityEngine;
 using System.Collections;
 
 public class RockSpawnScript_2 : MonoBehaviour {
 
     public GameObject[] rocks;
     public GameObject Rock_001;
     public GameObject Rock_002;
     public GameObject Rock_003;
     public GameObject Rock_004;
     public GameObject Rock_005;
     public GameObject Rock_006;
     public GameObject Rock_007;
     
     float timeElapsed = 0;
     float spawnCycle = 5f;
     int chooser = Random.Range (1,7);
 
     void update () 
     
     {
         timeElapsed += Time.deltaTime;
         if (timeElapsed > spawnCycle)
         {
 
             int[] array ={1, 2, 3, 4, 5, 6, 7};
 
             int chooser= UnityEngine.Random.Range(0, array.Length);
             
             GameObject temp;
 
             Vector3 pos = temp.transform.position;
 
             temp = Instantiate(rocks[Random.Range (1,7)]) as GameObject;
                 pos = temp.transform.position;
                 temp.transform.position = new Vector3(Random.Range(14, -14), pos.y, pos.z);
             }
         }
     }

So yeah, it doesn't solve the problem but atleast its cleaner, the issue as stated is "Use of unassigned local variable 'temp'. Not entirely sure where i'm going wrong on this.

cheers for your time mate I know i'm not the easiest when explaining a problem! :)

avatar image Phantomized · Oct 21, 2014 at 12:02 PM 0
Share

It's because you call Vector3 pos on temp when it's just an unassigned gameobject, so it doesn't have a transform or anything yet.

try:

 GameObject temp = Instantiate(rocks[Random.Range (1,7)]) as GameObject;
 
 Vector3 pos = temp.transform.position;

That way you declare and assign at the same time so you don't need to do stuff like the:

GameObject temp;

etcetera first. :) It's a waste of space.

avatar image JCGameArt · Oct 21, 2014 at 12:35 PM 0
Share

That definately solved the problem thankyou! :)

Though I have an issue arise, for some reason they simply are not spawning into the level. Not sure why the references are correct and so on, my hierarchy simply isn't cloning them in.

What could be the issue here?

 using UnityEngine;
 using System.Collections;
 
 public class RockSpawnScript : MonoBehaviour {
     
     public GameObject[] rocks;
     public GameObject Rock_001;
     public GameObject Rock_002;
     public GameObject Rock_003;
     public GameObject Rock_004;
     public GameObject Rock_005;
     public GameObject Rock_006;
     public GameObject Rock_007;
     
     float timeElapsed = 3;
     float spawnCycle = 5f;
     int chooser = Random.Range (1,7);
     
     void update () 
         
     {
         timeElapsed += Time.deltaTime;
         if (timeElapsed > spawnCycle)
         {
             
             int[] array ={1, 2, 3, 4, 5, 6, 7};
             
             int chooser= UnityEngine.Random.Range(0, array.Length);
             
             GameObject temp = Instantiate (rocks[Random.Range (1,7)]) as GameObject;
             
             Vector3 pos = temp.transform.position;
             
             temp = Instantiate(rocks[Random.Range (1,7)]) as GameObject;
             pos = temp.transform.position;
             temp.transform.position = new Vector3(Random.Range(14, -14), pos.y, pos.z);
         }
     }
 }


As far as I'm looking they should be instantiating across the axis specified and in good variety now. ~But null.

Cheers guys!

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Phantomized · Oct 21, 2014 at 01:02 PM

I took the liberty of cleaning your code up a bit. This should work, and I tell you what I changed after the //

 using UnityEngine;
 using System.Collections;
 
 
 public class RockSpawnScript : MonoBehaviour {
     
     public GameObject[] rocks;

     //I deleted all the single rock gameobjects. You don't need them when you have
     //an array called rocks where you can put them all inside in the editor

     float timeElapsed = 0; //Changed this to 0 because this is your "counter" and it should start at 0
     float spawnCycle = 5f;
     
     void Update () //You wrote update with a lowercase u here. So that's why it didn't work. Always remember to Capitalize methods.
         
     {
         timeElapsed += Time.deltaTime;
         if (timeElapsed > spawnCycle)
         {
             timeElapsed = 0; //Once it becomes true that timeElapsed is higher than our spawnCycle value, we reset it back to 0 so it can begin counting up again from 0.
             GameObject temp = Instantiate (rocks[Random.Range (0,rocks.Length)]) as GameObject; //Tells it to find a random rock between 0(the first rock in the rocks array) and the total length of your array.
             Vector3 pos = temp.transform.position;
             temp.transform.position = new Vector3(Random.Range(14, -14), pos.y, pos.z);
         }
     }
 }
     
     
     

Comment

People who like this

0 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 JCGameArt · Oct 21, 2014 at 01:44 PM 0
Share

This has solved it brilliantly, I worked out the "U" in update and 0 but was still stumped, you've just helped solve whats had me baffled for two weeks! Also thanks for the quick feedback and explaining i'll carry that forward to other scripts! :)

Thanks!

avatar image Phantomized · Oct 21, 2014 at 01:56 PM 0
Share

You're welcome. Good luck :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Camera follow script not following player? 1 Answer

How do i Instantiate a prefab with specific assests included 3 Answers

Need help with launching mechanic 0 Answers

SetActive() Object reference not set to an instance of an object. 1 Answer

How to create a Graph in Unity 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