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
0
Question by John Smith · Nov 25, 2010 at 11:33 PM · transformequal

Changeing a Transform var

So, my code goes somthing like this:

var ObjectA:Transform var ObjectB:Transform var ObjectC:Transform var InstantiateObject

function Update(){

if (FireRequirements){ var ClonedObject = Instantiate(InstantiateObject, transform.position, Quaternion.identity) }

if (SpawnObjectA){ InstantiateObject = ObjectA }

if(SpawnObjectB){ InstantiateObject = ObjectB }

if (SpawnObjectC){ InstantiateObject = ObjectC } }

My problem comes when I get an error saying that I haven't defined InstantiateObject to start off with. When I do define InstantiateObject to start with, it won't change to Objects A, B, or C.

Congratulations if you had the patience to read all of of that, And thanks in advance to anyone who helps.

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
1

Answer by The_r0nin · Nov 26, 2010 at 12:45 AM

First, this would have been easier to read if you had selected all of the code and then hit the "zeroes and ones" (i.e. code) button.

Secondly, try explicitly typing your object, like:

var instantiateObject: GameObject;

The other issue might be that you try to instantiate your object before you've selected which object it will be (you hit the Instantiate "if" before you hit the Select "if"s). If your code above is in that order in your program, you might want to reverse it. Personally, I don't know why you wouldn't just select and Instantiate at the same time:

if (fireRequirements){
    var clonedObject: GameObject = objectDefault; // set which object you want if none are selected.. i.e. the default
    if (spawnObjectA){
       clonedObject = Instantiate(objectA, transform.position, Quaternion.identity)
    }
    else{
        if (spawnObjectB){
           clonedObject = Instantiate(objectB, transform.position, Quaternion.identity)
        }
        else{
           if (spawnObjectC){
              clonedObject = Instantiate(objectC, transform.position, Quaternion.identity)
           }
        }
    }
}      

Another hint: variable names normally begin with a lower case letter and functions with an upper case letter (see my code). This isn't required, but it will help you at a glance (if you see "IsSpawned" you will know you forgot the "()" for the function, instead of not being sure you didn't mean the variable "isSpawned").

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 The_r0nin · Nov 26, 2010 at 12:48 AM 0
Share

Note that if all three "spawnObject"s are true (or not null), three different objects will be spawned with that code. If you want just one object to spawn, make a variable to set which object is desired (i.e. 1, 2, or 3) and use a switch/case to choose between them.

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

No one has followed this question yet.

Related Questions

Make camera 1 transform equal to camera 2 transform 1 Answer

the problem of equating float with transform rotation 1 Answer

Bayonet Action Transform 0 Answers

Resources on how to make the player move/rotate in the camera direction? 1 Answer

Lock Camera to be within a box type area? 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